Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Thinking in terms of bits

JavaScript Is Best Script
Joined
Dec 13, 2010
Messages
631
Reaction score
131
You do know that the minimum value of an unsigned byte is 0 and it's maximum is 255. But why? Let's investigate deeper, into bits.

Decomposition of unsigned bytes into a binary string
Every byte is eight bits long. Which means that every byte in the world can be represented using a binary string of length 8. For example,
00110100 = 52
10100100 = 164
11010101 = 213
11111111 = 255
As you can see, since the length of a byte is 8, the maximum value it can hold is 11111111, which if you convert back to base 10, is equals = 2^8 - 1 = 255. Which is why, it's maximum possible value is 255. Fairly simple. But this only applies for unsigned bytes.

Introduction to Two's complement
However, you've seen bytes whose values are negative (I.E. -122, -102, -35). These are called signed bytes. How is it possible to even differentiate a negative signed byte from a positive signed byte? We have to set a flag somewhere. Which is why, mathematicians and computer scientists came up with Two's complement. (One's complement can also be used, but it's less common AFAIK)

In Two's complement, the first bit (from the left) is used as a flag to indicate whether a signed byte is positive or negative. If the first bit is 1, the value is negative, else, the value is positive. For example,
01110101, 01100110, 00011010, 00011110 are all positive, while
11000101, 10110000, 11101011, 10001011 are all negative.

Two's complement for positive values
Now you may ask, how does Two complements represent number like 123, -123, 22, -13? It's fairly simple.
For positive values, everything works the same as unsigned bytes. HOWEVER, since the first bit (from the left) has been used by the signed byte as a sign indicator, only 7 bits can be used to represent the positive values. Which means,
00000001 = 1,
00001000 = 8,
00100000 = 32,
01000000 = 64,
01111111 = 127
As you can see, 1111111 is the highest positive value 7 bits can store, which is why the maximum value of a signed byte is 01111111 = 127.

Two's complement for negative values
But what about negative numbers? Very simple. Just remember this : The value represented by the last 7 bits (from the left) indicates how far away towards 0 the value is from the minimum possible value. Which means,
10000000 = -128 + 0 = -128
10000001 = -128 + 1 = -127
10001000 = -128 + 8 = -120
10100000 = -128 + 32 = -96
11000000 = -128 + 64 = -64
11111111 = -128 + 127 = -1

-1 to 0
Lets say you have a signed byte whose value is 11111111 = -1
If you add one to it, it becomes 100000000. But wait, a byte is supposed to be only 8 bits long!
Which is exactly why, the first bit from the left gets discarded away and all you're left with is 00000000 = 0

Overflows
Now that you've gotten a sense of Two's complement, we'll move on to understand how overflowing and underflowing works.

Let's say you have a signed byte whose current value is 01111111 = 127, the max possible positive value for a signed byte.
What happens when you add one to this byte?
Well, it goes normally : 01111111 + 1 = 10000000
But what do you notice? The first bit has became a 1! This indicates that the value is now negative. How negative is it? Simple, it's 0000000 = 0 away from the lowest possible value, -128. Which means that 127 + 1 = -128 in Two's complements. Ahah! There's where overflowing comes in.

Underflows
As for underflowing, it's the same.
You have a signed byte whose current value is 10000000 = -128. You decide to deduct one from it:
10000000 - 1 = 01111111
Which just turns out to become 127, the maximum possible value of a signed byte. There's underflowing for you.

Using bits for bigger data types
You've always heard of Int16, Int32, Int64, bla bla bla. But the numbers at the back of "Int" really only just tells you the number of bits that is used to represent data of these types. Let's take Int16 for example:
0000000000000001 = 1
0000000010000000 = 128
0000000101101101 = 365
0111111111111111 = 32767 // Thats the max positive value for an signed Int16, or Short
1000000000000000 = -32768
1010000000000000 = -24576
1111111111111111 = -1
As you can see, everything works exactly the same as a signed byte, just that now we get to have 16 bits instead of 8, and can hence store bigger values.

How are data stored in computers
You often ask how many bytes large a certain file is. But have you ever heard anyone ask "hey bro, this game i downloaded, it's 100 megabits big"? Well certainly not, because every bit is grouped and stored in a computer as bytes. Which means that even if you wanted to store just the number 1 in your computer, you can't store it as a bit, you have to store it as a byte, as 00000001. But how do we store data types more than 8 bits long? Simple : we split them up into bytes. For example, i wanna store the following 98273 into memory as a signed Int32.
98273 = 00000000000000010111111111100001
We then split the horrendous looking binary string into bytes (I.E. split them into groups of 8 bits), to get 4 bytes:
00000000000000010111111111100001 = 00000000 00000001 01111111 11100001
Which can easily be stored into memory as four separate bytes.
This is why the size of the data types are in multiples of eight, and you usually see on the web that says that an integer is 4 bytes long, etc.

We'll that's all for now :D I must apologize if you are unable to comprehend my English.
 
Back
Top