+ 3
an unsigned int can be twice as large as the signed int. explain how
can anybody help me out
4 odpowiedzi
+ 3
range of unsigned integer is 0 to 2^32 and range of signed integer is -(2^16) to (2^16)-1. so if u r talking about just positive number then we can say that unsigned integer is double. but both have same size....( here I take integer size of 4 byte )
+ 3
Here's a diagram of the ranges:
-2^31 to 2^32-1
<----------------0-------------------------------->
signed
[-----------------0----------------]
unsigned
[---------------------------------]
As you can see, their ranges are the same size, but signed integers can cover both negatives and positives, while unsigned ones can only be positive (but in a 2x larger range).
+ 1
An int is _not_ 2^32! It is _usually_ 2^32. An int is _actually_ 2^(architecture/compiler decision). Only fixed-width types have guarantees.
The range limit is simple math. Let's take a char which is always 8 bits (1 byte) on any arch. It can store 2^8 which is 256. If we count from 0, we get the range 0-255. From 1, we get 1-256. And from 2, we get 2-257. The maximum is 256 numbers. So when we include a negative range, we generally half it: -128 to 127. The range remains 256. There's a more complex detail about bits, specifically why it falls like that, but this should suffice for now. Here's the full story if you want:
https://en.m.wikipedia.org/wiki/Signedness
0
thank you, but I thought the same, but later found that my question is also possible !