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
110
votes
7 answers

what is the unsigned datatype?

I've seen this unsigned "typeless" type used a couple of times, but never seen an explanation for it. I suppose there's a corresponding signed type. Here's an example: static unsigned long next = 1; /* RAND_MAX assumed to be 32767 */ int…
hhaamu
  • 6,851
  • 3
  • 19
  • 13
105
votes
5 answers

Unsigned keyword in C++

Does the unsigned keyword default to a specific data type in C++? I am trying to write a function for a class for the prototype: unsigned Rotate(unsigned object, int count) But I don't really get what unsigned means. Shouldn't it be like unsigned…
Crystal
  • 28,460
  • 62
  • 219
  • 393
103
votes
6 answers

Signed/unsigned comparisons

I'm trying to understand why the following code doesn't issue a warning at the indicated place. //from limits.h #define UINT_MAX 0xffffffff /* maximum unsigned int value */ #define INT_MAX 2147483647 /* maximum (signed) int value */ /*…
Peter
  • 1,031
  • 2
  • 8
  • 4
93
votes
7 answers

What happens if I assign a negative value to an unsigned variable?

I was curious to know what would happen if I assign a negative value to an unsigned variable. The code will look somewhat like this. unsigned int nVal = 0; nVal = -5; It didn't give me any compiler error. When I ran the program the nVal was…
ckv
  • 10,539
  • 20
  • 100
  • 144
93
votes
5 answers

How to use the unsigned Integer in Java 8 and Java 9?

In the Oracle "Primitive data types" page, it mentions that Java 8 adds support for unsigned ints and longs: int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of −231 and a maximum value of…
Pabce
  • 1,429
  • 2
  • 13
  • 12
90
votes
12 answers

performance of unsigned vs signed integers

Is there any performance gain/loss by using unsigned integers over signed integers? If so, does this goes for short and long as well?
Flexo
  • 2,506
  • 3
  • 21
  • 32
88
votes
9 answers

How do I deal with "signed/unsigned mismatch" warnings (C4018)?

I work with a lot of calculation code written in c++ with high-performance and low memory overhead in mind. It uses STL containers (mostly std::vector) a lot, and iterates over that containers almost in every single function. The iterating code…
Andrew T
  • 5,549
  • 7
  • 43
  • 55
78
votes
3 answers

"strlen(s1) - strlen(s2)" is never less than zero

I am currently writing a C program that requires frequent comparisons of string lengths so I wrote the following helper function: int strlonger(char *s1, char *s2) { return strlen(s1) - strlen(s2) > 0; } I have noticed that the function returns…
Adrian Monk
  • 1,061
  • 9
  • 17
76
votes
7 answers

What is the difference between signed and unsigned variables?

I have seen these mentioned in the context of C and C++, but what is the difference between signed and unsigned variables?
Sam Becker
  • 19,231
  • 14
  • 60
  • 80
75
votes
3 answers

Unsigned double in C++?

Why doesn't C++ support unsigned double syntax?
lost3den
  • 815
  • 1
  • 6
  • 6
71
votes
16 answers

Unsigned short in Java

How can I declare an unsigned short value in Java?
maiky
  • 3,503
  • 7
  • 28
  • 28
64
votes
7 answers

Best way to convert a signed integer to an unsigned long?

For certain hash functions in Java it would be nice to see the value as an unsigned integer (e.g. for comparison to other implementations) but Java supports only signed types. We can convert a signed int to an "unsigned" long as such: public static…
maerics
  • 151,642
  • 46
  • 269
  • 291
61
votes
7 answers

Comparison operation on unsigned and signed integers

See this code snippet int main() { unsigned int a = 1000; int b = -1; if (a>b) printf("A is BIG! %d\n", a-b); else printf("a is SMALL! %d\n", a-b); return 0; } This gives the output: a is SMALL: 1001 I don't understand what's happening…
Gitmo
  • 2,366
  • 5
  • 25
  • 31
60
votes
7 answers

What is the best way to work around the fact that ALL Java bytes are signed?

In Java, there is no such thing as an unsigned byte. Working with some low level code, occasionally you need to work with bytes that have unsigned values greater than 128, which causes Java to interpret them as a negative number due to the MSB being…
Max
  • 1,528
  • 1
  • 11
  • 17
58
votes
4 answers

Is std::is_unsigned::value well defined?

I am wondering whether std::is_unsigned::value is well defined according to the standard or not? I ask the question because typename std::make_unsigned::type is not well defined.
Vincent
  • 57,703
  • 61
  • 205
  • 388
1
2
3
83 84