Looking at your computer screen right now you can see many different types of data. You can probably see images, maybe your listening to music, and you are reading this text.
All of these different types are made up of the same basic data type called a bit.
Which are represented by either a 1
or a 0
. However, bits are a little too small to
be useful to us. That's why we have groups of 8 bits called a byte.
Now these bytes are much more useful because they can represent numbers larger than just
0
or 1
. A byte can represent 256 values to be precise.
The question that should be asked next is: what are those 256 values? This depends on the sign of the byte(s) in question.
Unsigned binary numbers are the easiest to work with because we don't have to worry
about differentiating between positive and negative numbers. All unsigned numbers are
positive An unsigned Now we will try to calculate the Base-10 value of the following unsigned 8-bit
number: 10011001
.
We can line up the bits with a table of powers of two.
1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 |
---|---|---|---|---|---|---|---|
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
Next we take all the numbers that have a 1
above them and add them all together. So we end
up with 128 + 16 + 8 + 1
which adds up to 153
.
Now, why does this work?
Well, a binary digit has only 2 possible values. So we can raise 2 to the nth digit to get
the value of only that digit. So, we have a bunch of digits that are 1
s we can do this
operation on all of the digits and sum the results to get the number it represents.
However, signed numbers are a little more complex than unsigned numbers. Signed numbers can represent numbers ranging from -127 to 127. However, the left-most bit of signed data determines the sign.
But it also gets a little weird. We would expect the binary number 00000001
to be 1, which would
be a correct assumption! Well, what about -1? You would think that we could just flip the furthest left bit to
a 1, however this is an incorrect assumption. The number 10000001
results in a value of -127.
We can calculate the value of a an unsigned number by using the same method as before. Only this time we have to pay closer attention to the value of the left-most binary digit.
1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 |
---|---|---|---|---|---|---|---|
-128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
We can construct the table in the same way as we did before, except the value that represents the sign bit is going to be negative.
So, now we can go through and add up the values from the table same as we did before. -128+16+8+1
which adds up to -103.
https://wandbox.org/permlink/GtXKIsfOS7yZIgH5
This link will take you to a code sandbox where you can experiment with code without having to install anything on your computer. I will be using this site for code examples in these lessons.
To modify the example click a button that says "Clone & Edit".
When you open the link you may think "Hey, this Java looks weird". That's because this is C, not Java. The reason I used C was because Java has some limitations that would have made this example much more complex than it needed to be. Unless similar low-level topics are covered you probably will not see more C-code in future Java lessons.
In this example feel free to change the values of the variables a
and b
to see how the binary values change as you change the numbers. You can also set it to a binary
value by typing 0b
, and then a series of eight 1
s and 0
s.
If you enter more, or less, digits you may get a value you didn't expect
Happy Coding!
-Solomon W. (Apr 23, 2023)