Questions tagged [unsigned-integer]

A specific data type that uses all its bits to represent an integer value, consequently only 0 or a positive number.

In typed languages, an unsigned integer is a specific data type that uses all its bits to represent an integer value, consequently only 0 or a positive number. It can also represent integers twice the magnitude of the signed integers for the same width since no bit is used up to indicate the sign.

591 questions
32
votes
1 answer

Why is this function call ambiguous?

I'm reading the standard and trying to figure out why this code won't be resolved without a cast. void foo(char c) { } // Way bigger than char void foo(unsigned long int) { } int main() { foo(123456789); // ambiguous foo((unsigned long int)…
30
votes
4 answers

Why not enforce 2's complement in C++?

The new C++ standard still refuses to specify the binary representation of integer types. Is this because there are real-world implementations of C++ that don't use 2's complement arithmetic? I find that hard to believe. Is it because the committee…
TonyK
  • 16,761
  • 4
  • 37
  • 72
30
votes
3 answers

How to prevent implicit conversion from int to unsigned int?

Suppose you have this: struct Foo { Foo(unsigned int x) : x(x) {} unsigned int x; }; int main() { Foo f = Foo(-1); // how to get a compiler error here? std::cout << f.x << std::endl; } Is it possible to prevent the implicit…
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
28
votes
9 answers

Convert unsigned int to signed int C

I am trying to convert 65529 from an unsigned int to a signed int. I tried doing a cast like this: unsigned int x = 65529; int y = (int) x; But y is still returning 65529 when it should return -7. Why is that?
darksky
  • 20,411
  • 61
  • 165
  • 254
28
votes
13 answers

Calculating bits required to store decimal number

Consider unsigned integer representation. How many bits will be required to store a decimal number containing: i) 3 digits ii) 4 digits iii) 6 digits iv) n digits I know that the range of the unsigned integer will be 0 to 2^n but I don't get how…
user379888
28
votes
3 answers

Arithmetic bitwise shift right "a shr b" with signed integers that stored in variables – wrong results! Internal Delphi’s bug?

I have a question (or more likely a bug report) about bit shifting behavior in Delphi (tested in Borland Delphi 7). Target: perform an "Arithmetic" bitwise shift right with any number. This means that sign-bit must be extended – binary number will…
aleksusklim
  • 361
  • 3
  • 11
24
votes
3 answers

JNI: converting unsigned int to jint

How do I convert an unsigned int to jint? Do I have to convert it at all, or can I just return it without any special treatment? This is basically my code right now, but I can't test it, as I haven't setup JNI locally. JNIEXPORT jint…
Pedro
  • 4,100
  • 10
  • 58
  • 96
23
votes
7 answers

C - unsigned int to unsigned char array conversion

I have an unsigned int number (2 byte) and I want to convert it to unsigned char type. From my search, I find that most people recommend to do the following: unsigned int x; ... unsigned char ch = (unsigned char)x; Is the right approach? I ask…
Jake
  • 16,329
  • 50
  • 126
  • 202
22
votes
4 answers

What is going on with bitwise operators and integer promotion?

I have a simple program. Notice that I use an unsigned fixed-width integer 1 byte in size. #include #include #include int main() { uint8_t x = 12; std::cout << (x << 1) << '\n'; std::cout << ~x; …
22
votes
2 answers

Is comparing an underflowed, unsigned integer to -1 well-defined?

Consider the following†: size_t r = 0; r--; const bool result = (r == -1); Does the comparison whose result initialises result have well-defined behaviour? And is its result true, as I'd expect? This Q&A was written because I was unsure of two…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
21
votes
3 answers

Overflowing of Unsigned Int

What will the unsigned int contain when I overflow it? To be specific, I want to do a multiplication with two unsigned ints: what will be in the unsigned int after the multiplication is finished? unsigned int someint = 253473829*13482018273;
GILGAMESH
  • 1,816
  • 3
  • 23
  • 33
21
votes
5 answers

Is a negative integer summed with a greater unsigned integer promoted to unsigned int?

After getting advised to read "C++ Primer 5 ed by Stanley B. Lipman" I don't understand this: Page 66. "Expressions Involving Unsigned Types" unsigned u = 10; int i = -42; std::cout << i + i << std::endl; // prints -84 std::cout << u + i <<…
Alex24
  • 600
  • 2
  • 13
21
votes
3 answers

Why doesn't Kotlin support unsigned integers?

I came across a situation just recently in which an unsigned integer would have been really useful (e.g. any negative value would not make sense etc.). Surprisingly, I discovered that Kotlin does not support unsigned integers - and there doesn't…
starbeamrainbowlabs
  • 5,692
  • 8
  • 42
  • 73
21
votes
3 answers

How to count leading zeros in a 32 bit unsigned integer

Could anyone tell me please what is an efficient algorithm to count the number of leading zeroes in a 32-bit unsigned integer in C programming?
rzmuc
  • 243
  • 1
  • 5
  • 10
19
votes
5 answers

Unsigned integers in C++ for loops

I have made some research on Stackoverflow about reverse for loops in C++ that use an unsigned integer instead of a signed one. But I still do NOT understand why there is a problem (see Unsigned int reverse iteration with for loops). Why the…
Benjamin
  • 366
  • 1
  • 3
  • 8
1
2
3
39 40