Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is int16 type limit 32,768 if 16 bits max is 65,535?

Tags:

c#

clr

Sum of 16 bits all "1" bits will result 65,535:

first byte: 1(128) 1(64) 1(32) 1(16) 1(8) 1(4) 1(2) 1(1)

second byte: 1(327,68) 1(16,384) 1(8,192) 1(4,096) 1(2,048) 1(1,024) 1(512) 1(256)

which the decimal would be:

32,768 + 16,384 + 8,192 + 4,096 + 2,048 + 1,024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1

total is: 65,535

the max of int16 should be 65,535 and 32,768(which is just 1000 0000 0000 0000)

I can't see how it would be otherwise.

like image 914
RollRoll Avatar asked Jun 29 '17 15:06

RollRoll


5 Answers

Because it ranges from -32,768 to 32,767, which in sum is 65,536 (0 is included here) possible values.

If you use UInt16 (which stands for unsigned int16) you can give it a value of 65,535.

like image 173
Romano Zumbé Avatar answered Oct 16 '22 17:10

Romano Zumbé


There is a difference between a signed and an unsigned integer. One has the ability to be negative and the other one doesn't.

It also is not just for int16, but for other types as well.

+--------+----------------------------+----------------------------+--------------------------+
|  Type  |            Min             |            Max             |           Size           |
+--------+----------------------------+----------------------------+--------------------------+
| sbyte  | -128                       | 127                        | Signed 8-bit integer     |
| byte   | 0                          | 255                        | Unsigned 8-bit integer   |
| char   | U+0000                     | U+ffff                     | Unicode 16-bit character |
| short  | -32,768                    | 32,767                     | Signed 16-bit integer    |
| ushort | 0                          | 65,535                     | Unsigned 16-bit integer  |
| int    | -2,147,483,648             | 2,147,483,647              | Signed 32-bit integer    |
| uint   | 0                          | 4,294,967,295              | Unsigned 32-bit integer  |
| long   | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807  | Signed 64-bit integer    |
| ulong  | 0                          | 18,446,744,073,709,551,615 | Unsigned 64-bit integer  |
+--------+----------------------------+----------------------------+--------------------------+
like image 41
Xorifelse Avatar answered Oct 16 '22 16:10

Xorifelse


That's difference between signed int and unsigned int. 16-bit unsigned int ranges in [0, 65535]. While 16-bit signed int ranges [-32768, 32767]. The max of int16 should be 32767 instead of 32768.

like image 38
lwshang Avatar answered Oct 16 '22 17:10

lwshang


Int16 is signed. The most significant bit denotes sign (0 for positive, 1 for negative). The highest number achievable with 0 as MSB is 0111 1111 1111 1111, which is 32767 in decimal.

It is designed this way, so that unsigned addition/subtraction would work on signed numbers as well. So, to get -1, you subtract 1 from 0, and get 1111 1111 1111 1111 (due to overflow). If you keep subtracting, you eventually reach 1000 0000 0000 0000 = -32768 (dec), which is the lowest negative number representable as int16.

So, 2^16 = 65536, which is the total number of representable values. This adds up from: (a) 1 value for zero, (b) 32767 positive numbers, (c) 32768 negative numbers.

like image 2
Surak of Vulcan Avatar answered Oct 16 '22 16:10

Surak of Vulcan


You're forgetting the sign. int16 is a signed type, so it's actually:

first byte: ...
second byte: sign(+ or -) 1(16,384) ...

Which covers the range from -32768 to 32767 (0 is considered a positive).
Most of the time, 0 is used for positive and 1 for negative: Sign bit

It would be an entirely different matter if it was a uint16 (u for unsigned):

first byte: ...
second byte: 1(32768) ...

Which, in turn, ranges from 0 to 65535.

like image 1
Dasanko Avatar answered Oct 16 '22 18:10

Dasanko