
Data Representation
| Created |
|---|
Computers represent Data in Binary: a base system that deals with 1s and 0s. (on and off)
For example, in the practical world you can count to 31 with your 5 fingers and this shows how a computer can count as well.
Positive integers:
The value of each bit is calculated by multiplying the previous one by 2. That is why it is 2⁰, 2¹, 2², 2³, 2⁴, 2⁵, 2⁶, 2⁷ for the actual numbers.
We usually use 8 bit binary which can count from 0 to 255, making 256 possible combinations from 00000000 to 11111111
+/- integers:
Things become more difficult when using positive and negative integers as you have to find a way of using one of the bits to determine whether the number is +/-.
That is why twos complement is used to fix this problem, as the leftmost bit is used for this reason.
If the leftmost bit is 1, then the number is negative and it will be -2. The leftmost bit has the value -2ⁿ⁻¹, where n is the number of bits.
If the leftmost bit is 0, then carry on as normal.
Advantages of twos complement:
- There is only one zero, instead of solely using the leftmost bit as a sign bit we assign it a value here.
- Addition and subtraction are easy to implement: this is because to convert a number to a negative in twos compoelemt you just flip all the 1s to zeros and vice versa and then subsequently add one. then just add the positive integer to the twos complement of the negative integer.
So A - B = A + (twos complement of B)

