Search code examples
c#clr

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


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.


Solution

  • 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.