Questions tagged [unsigned]

An unsigned variable is a variable that can only represent non-negative numbers.

An unsigned variable is a variable that can only represent non-negative numbers, as opposed to a signed variable which can represent both positive and negative numbers.

1251 questions
491
votes
18 answers

Iteration over std::vector: unsigned vs signed index variable

What is the correct way of iterating over a vector in C++? Consider these two code fragments, this one works fine: for (unsigned i=0; i < polygon.size(); i++) { sum += polygon[i]; } and this one: for (int i=0; i < polygon.size(); i++) { sum…
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
445
votes
7 answers

How to printf "unsigned long" in C?

I can never understand how to print unsigned long datatype in C. Suppose unsigned_foo is an unsigned long, then I try: printf("%lu\n", unsigned_foo) printf("%du\n", unsigned_foo) printf("%ud\n", unsigned_foo) printf("%ll\n",…
bodacydo
  • 75,521
  • 93
  • 229
  • 319
445
votes
17 answers

Signed versus Unsigned Integers

Am I correct to say the difference between a signed and unsigned integer is: Unsigned can hold a larger positive value and no negative value. Unsigned uses the leading bit as a part of the value, while the signed version uses the left-most-bit to…
ASDFdotASPX
404
votes
10 answers

Declaring an unsigned int in Java

Is there a way to declare an unsigned int in Java? Or the question may be framed as this as well: What is the Java equivalent of unsigned? Just to tell you the context I was looking at Java's implementation of String.hashcode(). I wanted to test the…
Harshdeep
  • 5,614
  • 10
  • 37
  • 45
403
votes
17 answers

Why doesn't Java support unsigned ints?

Why doesn't Java include support for unsigned integers? It seems to me to be an odd omission, given that they allow one to write code that is less likely to produce overflows on unexpectedly large input. Furthermore, using unsigned integers can…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
212
votes
17 answers

Can we make unsigned byte in Java

I am trying to convert a signed byte in unsigned. The problem is the data I am receiving is unsigned and Java does not support unsigned byte, so when it reads the data it treats it as signed. I tried it to convert it by the following solution I got…
dln
  • 2,155
  • 2
  • 13
  • 8
176
votes
8 answers

Difference between signed / unsigned char

So I know that the difference between a signed int and unsigned int is that a bit is used to signify if the number if positive or negative, but how does this apply to a char? How can a character be positive or negative?
Chiggins
  • 8,197
  • 22
  • 56
  • 81
158
votes
13 answers

Why doesn't C have unsigned floats?

I know, the question seems to be strange. Programmers sometimes think too much. Please read on... In C I use signed and unsigned integers a lot. I like the fact that the compiler warns me if I do things like assigning a signed integer to an unsigned…
Nils Pipenbrinck
  • 83,631
  • 31
  • 151
  • 221
151
votes
6 answers

What is the difference between “int” and “uint” / “long” and “ulong”?

I know about int and long (32-bit and 64-bit numbers), but what are uint and ulong?
Difference Engine
  • 12,025
  • 5
  • 21
  • 11
149
votes
7 answers

Difference between size_t and unsigned int?

I am so confused about size_t. I have searched on the internet and everywhere mentioned that size_t is an unsigned type so, it can represent only non-negative values. My first question is: if it is used to represent only non-negative values, why…
Vikas Verma
  • 3,626
  • 6
  • 27
  • 40
122
votes
6 answers

Is unsigned integer subtraction defined behavior?

I have come across code from someone who appears to believe there is a problem subtracting an unsigned integer from another integer of the same type when the result would be negative. So that code like this would be incorrect even if it happens to…
user14554
120
votes
2 answers

Why is std::ssize() introduced in C++20?

C++20 introduced the std::ssize() free function as below: template constexpr auto ssize(const C& c) -> std::common_type_t>; A possible…
John Z. Li
  • 1,893
  • 2
  • 12
  • 19
119
votes
10 answers

Java equivalent of unsigned long long?

In C++, I enjoyed having access to a 64 bit unsigned integer, via unsigned long long int, or via uint64_t. Now, in Java longs are 64 bits, I know. However, they are signed. Is there an unsigned long (long) available as a Java primitive? How do I use…
eleven81
  • 6,301
  • 11
  • 37
  • 48
116
votes
2 answers

Should I use size_t or ssize_t?

At my code, I do not use int or unsigned int. I only use size_t or ssize_t for portable. For example: typedef size_t intc; // (instead of unsigned int) typedef ssize_t uintc; // (instead of int) Because strlen, string, vector... all use…
hgyxbll
  • 1,317
  • 2
  • 9
  • 9
114
votes
6 answers

How to convert signed to unsigned integer in python

Let's say I have this number i = -6884376. How do I refer to it as to an unsigned variable? Something like (unsigned long)i in C.
Lior
  • 5,841
  • 9
  • 32
  • 46
1
2 3
83 84