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
35
votes
3 answers

What happens when I mix signed and unsigned types in C++?

I have some doubt about type conversion, could you explain me what happens in an expression like this: unsigned int u = 10; int a = -42; std::cout << u - a << std::endl; Here I know that the result will be 52 if I apply the rules when we have two…
Piero Borrelli
  • 1,151
  • 2
  • 11
  • 15
34
votes
3 answers

Unsigned hexadecimal constant in C?

Does C treat hexadecimal constants (e.g. 0x23FE) as signed or unsigned integers?
Amr Bekhit
  • 4,613
  • 8
  • 34
  • 56
34
votes
1 answer

Why is std::streamsize defined as signed rather than unsigned?

According to http://en.cppreference.com/w/cpp/io/streamsize The type std::streamsize is a signed integral type used to represent the number of characters transferred in an I/O operation or the size of an I/O buffer. As far as I can imagine, a…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
34
votes
9 answers

How to get the signed integer value of a long in python?

If lv stores a long value, and the machine is 32 bits, the following code: iv = int(lv & 0xffffffff) results an iv of type long, instead of the machine's int. How can I get the (signed) int value in this case?
Paul Oyster
32
votes
5 answers

Is there a Java library for unsigned number type wrappers?

Obviously, Java doesn't support unsigned number types natively, and that's not going to change soon (comments starting in 2002). However, when working with databases, such as MySQL, they may come in handy every now and then. There are a lot of…
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
32
votes
3 answers

What happens when I assign a negative value to an unsigned int?

Possible Duplicate: signed to unsigned conversion in C - is it always safe? Let's say I declare a variable of type unsigned int : unsigned int x = -1; Now -1 in two's complement (assuming 32 bit machine) is 0xFFFFFFFF. Now when I assigned this…
n0nChun
  • 720
  • 2
  • 8
  • 21
32
votes
3 answers

Unsigned long in Java

Currently, I am using signed values, -2^63 to 2^63-1. Now I need the same range (2 * 2^64), but with positive values only. I found the java documentations mentioning unsigned long, which suits this use. I tried to declare 2^64 to a Long wrapper…
Annapoorni D
  • 831
  • 2
  • 13
  • 30
31
votes
4 answers

Why does 'auto' not respect the unary minus operator?

I'm quite new to C++ but I find this behaviour of auto weird: class A{}; int main() { A a; auto x = -(sizeof(a)); cout << x << endl; return 0; } Variable x is unsigned in this case although I used the unary minus operator at the…
CodeShark
  • 1,709
  • 2
  • 14
  • 22
31
votes
3 answers

MySQL TINYINT as unsigned

I tried to write t=t|128 but got an out of range error. I suspect tinyint is signed. However http://dev.mysql.com/doc/refman/5.5/en/numeric-types.html says it may be unsigned (below). But it doesn't say how to make it unsigned. How do I? Type …
user34537
30
votes
4 answers

Why is (18446744073709551615 == -1) true?

When I was working on string::npos I noticed something and I couldn't find any explanation for it on the web. (string::npos == ULONG_MAX) and (string::npos == -1) are true. So I tried this: (18446744073709551615 == -1) which is also true. How…
Bahadır
  • 454
  • 1
  • 4
  • 8
30
votes
3 answers

Unsigned values in C

I have the following code: #include int main() { unsigned int a = -1; int b = -1; printf("%x\n", a); printf("%x\n", b); printf("%d\n", a); printf("%d\n", b); printf("%u\n", a); printf("%u\n", b); …
rvillablanca
  • 1,606
  • 3
  • 21
  • 34
30
votes
3 answers

Does cast between signed and unsigned int maintain exact bit pattern of variable in memory?

I want to pass a 32-bit signed integer x through a socket. In order that the receiver knows which byte order to expect, I am calling htonl(x) before sending. htonl expects a uint32_t though and I want to be sure of what happens when I cast my…
Flash
  • 15,945
  • 13
  • 70
  • 98
30
votes
6 answers

printf format for unsigned __int64 on Windows

I need to print a ULONGLONG value (unsigned __int64). What format should i use in printf ? I found %llu in another question but they say it is for linux only. Thanks for your help.
Virus721
  • 8,061
  • 12
  • 67
  • 123
29
votes
7 answers

How to cast or convert an unsigned int to int in C?

My apologies if the question seems weird. I'm debugging my code and this seems to be the problem, but I'm not sure. Thanks!
Eric Brotto
  • 53,471
  • 32
  • 129
  • 174
29
votes
1 answer

Can't get rid of "this decimal constant is unsigned only in ISO C90" warning

I'm using the FNV hash as a hashing algorithm on my Hash Table implementation but I'm getting the warning in the question title on this line: unsigned hash = 2166136261; I don't understand why this is happening because when I do…
rfgamaral
  • 16,546
  • 57
  • 163
  • 275