Computer Number Systems
Computer Number Systems
Number Systems
Dr Robert Harle
Today's Topics
The significance of the bit and powers of 2
Data quantities (B, kB, MB, GB, etc)
Number systems (decimal, binary, octal , hexadecimal)
Representing negative numbers (sign-magnitude, 1’s
complement, 2’s complement)
Binary addition (carries, overflows)
Binary subtraction
So...
What is a bit?
The Significance of the Bit
A bit (Binary digIT) is merely 0 or 1
It is a unit of information since you cannot communicate
with anything less than two states
The use of binary encoding dates back to the 1600s with
Jacquard's loom, which created textiles using card
templates with holes that allowed needles through
Bits and Computers
The nice thing about a bit is that, with only two states, it is
easy to embody in physical machinery
Each bit is simply a switch and computers moved from
vacuum tubes to transistors for this
e-
Decimal Number System
Most computers count in binary, which we can easily
understand from the decimal so ingrained in us
35462
Decimal Number System
Most computers count in binary, which we can easily
understand from the decimal so ingrained in us
2x100
35462
Decimal Number System
Most computers count in binary, which we can easily
understand from the decimal so ingrained in us
6x101+2x100
35462
Decimal Number System
Most computers count in binary, which we can easily
understand from the decimal so ingrained in us
4x102+ 6x101+2x100
35462
Decimal Number System
Most computers count in binary, which we can easily
understand from the decimal so ingrained in us
5x103+4x102+ 6x101+2x100
35462
Decimal Number System
Most computers count in binary, which we can easily
understand from the decimal so ingrained in us
35462
Binary
Binary is exactly the same, only instead of ten
digits/states (0 to 9) we have just two, so the base
becomes 2:
10110
Binary
Binary is exactly the same, only instead of ten
digits/states (0 to 9) we have just two, so the base
becomes 2:
10110b = 22d
Binary
Binary is exactly the same, only instead of ten
digits/states (0 to 9) we have just two, so the base
becomes 2:
10110b = 22d
35.462
Works for Fractional Numbers too...
35.462
10.110b = 2.75d
Check
11.011b
Check
11.011b = 3.375d
Representable Numbers
With d decimal digits, we can represent 10d different
values, usually the numbers 0 to (10d-1) inclusive
In binary with n bits this becomes 2n values, usually
the range 0 to (2n-1)
001 111
+ 001 + 001
111 000
000
110 001 001
010
101 010 011
100 011
100
101
110
111
-7 = 1111
Normal positive
Negative representation of 7
1101
+ 0001
1110
1101 +13
+ 0001 +1
1110 +14
Unsigned
interpretation
-5 1101 +13
+1 + 0001 +1
-6 1110 +14
Sign-mag Unsigned
interpretation interpretation
1101 +13
+0001 +1
1110 +14
Unsigned
-3 1101 +13
+1 +0001 +1
-2 1110 +14
2's-comp Unsigned
-3 1101 +13
+1 +0001 +1
-2 1110 +14
2's-comp Unsigned
0100 +4
Carry flag: 0
+0100 +4
1000 +8
Unsigned
Signed Addition
So we can use the same circuitry for unsigned and 2s-
complement addition :-)
Well, almost.
+4 0100 +4
Carry flag: 0
+4 +0100 +4
-8 1000 +8
2's-comp Unsigned
The problem is our MSB is now signifying the sign and our carry
should really be testing the bit to its right :-(
Signed Addition
So we can use the same circuitry for unsigned and 2s-
complement addition :-)
Well, almost.
+4 0100 +4
Carry flag: 0
+4 +0100 +4 Overflow: 1
-8 1000 +8
2's-comp Unsigned
The problem is our MSB is now signifying the sign and our carry
should really be testing the bit to its right :-(
So we introduce an overflow flag that indicates this problem
Integer subtraction
Could implement the “borrowing”
algorithm you probably learnt in school
But why bother? We can just add the
2's complement instead.
0100 0100
- 0011 → +1101
0001
Flags Summary
When adding/subtracting
Carry flag → overflow for unsigned integer
Overflow flag → overflow for signed integer