Tuesday, April 16, 2013

Binary, bits, bytes, short, long, and integer.

Recently I took a class on networking and had never really put any thought into binary, but found it interesting.  As some know, a byte is 256 bits of information in the range of 0-255.  Below is considered an octet because it is 8 bits, yeah, 8 bits like your old NES.  Below is the breakdown of how it works.

Ok, now that we understand the byte, we can understand how to use a byte in java.  Still the same 8 bits of information, but a range of –128 to 127 for a total of 256.  Makes sense, let's continue.

Now, with that in mind, let's compare it to a short.  Yeah, I know, short isn't that common, but is 16 bit, or 2 octets.  This means it has a range of –32,768 to 32,767.  Below is the double octet that represents it.  Notice how the number keeps doubling in the next octet.

Since that makes sense now, it only makes sense that an integer being 32 bit, is basically 4 octets that will continue doubling until it reaches a range of  -2,147,483,648 to 2,147,483,647.

At this point you can understand that a 64 bit long has a range of –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

byte=–128 to 127;
short=–32,768 to 32,767
integer=-2,147,483,648 to 2,147,483,647
long=–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807