BINARY NUMBERS
Binary Digits (bits)
• data within a computer system are stored in one of 2 physical states (hence the use of binary digits)
• 0V and 5V
• charge / NO charge on a transistor gate
• ferrite core magnetised clockwise or counter clockwise •…
• binary digits (bits) and are represented by the values 0 and 1
• binary digits are normally grouped together so they are easier to work with
intel
16 bits = WORD
32 bits = DWORD (double word)
• 4 bits = nibble or nybble
• 8 bits = byte (or 2 nibbles)
• 16 bits = halfword (or 2 bytes)
• 32 bits = word (or 4 bytes)
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 18-Sep-18
1110 01101001 01111000 01001011
11101101 01111101 01100111 01100101
1
BINARY NUMBERS
Unsigned Binary Integers
• unsigned == positive integers ONLY
• converting binary to decimal
base 2 (assume base 10 if not specified)
1001012
25=32 24=16 23=8 22=4 21=2 20=1
1001012 = 1×25 + 0×24 + 0×23 + 1×22 + 0×21 + 1×20 1001012 = 32 + 0 + 0 + 4 + 0 + 1 1001012 = 37
• similar decimal calculation (base 10) 37 = 3×101 + 7×100
403 = 4×102 + 0×101 + 3×100
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 18-Sep-18
2
BINARY NUMBERS
Converting a positive decimal integer to binary
• keep dividing by 2 until 0 and remember remainders
• convert 37 to binary
2 37
2 18 1 2 90 2 41 220 210
01
• what is 42 in binary?
• what is 16 in binary?
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 18-Sep-18
37 ÷ 2 = 18 remainder 1
read from bottom
37 = 0010 01012
0010 10102
0001 00002
3
BINARY NUMBERS
• pictorial view of a 4 bit unsigned binary integer
• 4 bit unsigned binary integer range
00002 to 11112 0 to 15
• n bit unsigned binary integer range
0 to 2n-1
1111 0000 15 0000
0001
1110 1101
13
1
14 2
0010
0011
3
1100 12
11 5
4 0100
1011
0101
10 6
1010 9 8 7 0110
1001 1000 0111
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 18-Sep-18
4
BINARY NUMBERS
There are 102types of people in the world: those who understand binary and those who don’t.
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 18-Sep-18
5
BINARY NUMBERS
Signed Binary Integers
• SIGNED = positive and negative integers
• 2’s complement notation
• convert +5 to -5 by taking the 2’s complement (invert bits and add 1)
5
01012
invert bits
10102
add 1
00012
-5
10112
invert bits
01002
add 1
00012
5
01012
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 18-Sep-18
6
BINARY NUMBERS
Signed Binary Integers
• same effect achieved by subtracting from 0 (modulo 16 in this case)
zero
00002
subtract 5
01012
-5
10112
ignore bits beyond the first 4
• 4 bit signed binary integer
• positive range 00002 to 01112 0 to 7
• negative range 11112 to 10002 -1 to -8
• n bit signed binary integer range: -2n-1 to 2n-1 – 1
• most significant bit (MSB) indicates sign (0 – positive, 1 – negative)
• note asymmetrical range – only one zero (do have a +0 and a -0) CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 18-Sep-18
7
BINARY NUMBERS
• pictorial view of a 4 bit signed binary integer
• if unsigned, inner ring would have values 0 to 15
• value depends whether the binary numbers are interpreted as unsigned or signed (the programmer should know!)
• 2’s complement notation used because the same CPU hardware can perform unsigned and signed binary arithmetic simultaneously
1110
0000
1111 0001
-1 0000 1 0010 -2 2
1101
1100
1011
-3 3
0011
4 0100
-4
-5 5
-6 6
0101
1010
-7 -8
7
0110
1001
1000
0111
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 18-Sep-18
8
BINARY NUMBERS
Try these
• what is -42 in binary? +42
invert bits and add 1
• what is -16 in binary? +16
invert bits and add 1
0010 10102 1101 01102
0001 00002 1111 00002
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 18-Sep-18
9
BINARY NUMBERS
BINARY
DEC
HEX
0000
0
0
0001
1
1
0010
2
2
0011
3
3
0100
4
4
0101
5
5
0110
6
6
0111
7
7
1000
8
8
1001
9
9
1010
10
A
1011
11
B
1100
12
C
1101
13
D
1110
14
E
1111
15
F
Hexadecimal Notation
• base 16
• easier to handle large binary numbers by grouping 4 binary bits into a hexadecimal digit (starting at the least significant bit)
• consider the following 16 bit unsigned binary integer 1111 1010 1100 11102 = FACE16
F×163 + A×162 + C×161 + E×160 15×163 + 10×162 + 12×161 + 14×160 15×4096 + 10×256 + 12×16 + 14×1 64,206
• what about?
0000 1011 1010 11012 = 0BAD16
0×163 + 11×162 + 10×161 + 13×160
2,989
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 18-Sep-18
10
BINARY NUMBERS
BINARY
DEC
HEX
0000
0
0
0001
1
1
0010
2
2
0011
3
3
0100
4
4
0101
5
5
0110
6
6
0111
7
7
1000
8
8
1001
9
9
1010
10
A
1011
11
B
1100
12
C
1101
13
D
1110
14
E
1111
15
F
Try this
• what decimal value is FACE16 if interpreted as a 16 bit signed integer?
• MSB is 1, hence negative so take 2’s complement by inverting bits and adding 1
FACE16
invert bits
053116
add 1
053216
• convert 053216 to decimal 0×163 + 5×162 + 3×161 + 2×160
1,330
• FACE16 when interpreted as a 16 bit signed integer = -1,330
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 18-Sep-18
11
BINARY NUMBERS
BINARY
0010
0011
0100
0101
0110
DEC
2
HEX
0000
0001
0
1
0
1
2
3
4
3
4
5
5
6
6
0111
1000
7
7
8
8
1001
9
9
1010
10
A
1011
11
B
1100
12
C
1101
1110
1111
13
14
15
D
E
Decimal to hexadecimal conversion
• •
convert 20,085 to hexadecimal
keep dividing by 16 until 0 and remember remainders
16 20085
16 1255 5
20085 ÷ 16 = 1255 remainder 5
16 78 16 4 0
readfrom 7 bottom
E 4
20,085 = 4E7516
•
convert -20,085 to hexadecimal (assume 16 bit signed integer)
20,085
4E7516
invert bits
B18A16
add 1
000116
-20,085
B18B16
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 18-Sep-18
F12
BINARY NUMBERS
Alternative notation
• when writing ARM Assembly Language, can use the following notion for decimal, hexadecimal and binary integers
1000
no prefix usually means decimal
0x1000
hexadecimal (also used by C/C++ and Java)
&1000
alternative hexadecimal notation
2_1000
binary
n_1000
base n eg. 8_777 is octal (base 8)
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 18-Sep-18
13
BINARY NUMBERS
BINARY
0010
0011
0100
0101
0110
DEC
2
HEX
0000
0001
0
1
0
1
2
3
4
3
4
5
5
6
6
0111
1000
7
7
8
8
1001
9
9
1010
10
A
1011
11
B
1100
12
C
1101
1110
1111
13
14
15
D
E
Adding Hexadecimal Numbers • compute 0xA89F + 0x09A1
unsigned
signed
A89F16
43,167
-22,369
+
09A116
2,465
2,465
B24016
45,632
-19,904
• remember hexadecimal/binary numbers can be interpreted as being unsigned or signed
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 18-Sep-18
F14
BINARY NUMBERS
BINARY
0010
0011
0100
0101
0110
DEC
2
HEX
0000
0001
0
1
0
1
2
3
4
3
4
5
5
6
6
0111
1000
7
7
8
8
1001
9
9
1010
10
A
1011
11
B
1100
12
C
1101
1110
1111
13
14
15
D
E
Subtracting Hexadecimal Numbers • compute 0xA89F – 0x09A1
unsigned
signed
A89F16
43,167
-22,369
–
09A116
2,465
2,465
9EFE16
40,702
-24,834
• remember hexadecimal/binary numbers can be interpreted as being unsigned or signed
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 18-Sep-18
F15
BINARY NUMBERS
Real Binary Numbers
• binary point (rather than a decimal point)
• what is the value of the following binary number? 11.0101012
21=2 20=1 2-1=0.5 2-2=0.25 2-3=0.125 2-4=0.0625 …
• 2 + 1 + 0.25 + 0.0625 + 0.015625 = 3.328125
• shows how real numbers can be represented as floating point binary numbers inside a computer, but further detail is beyond the scope of CS1021
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 18-Sep-18
16
BINARY NUMBERS
Larger units
• larger units of information
1 kilobyte (KB) = 210 bytes = 1,024 bytes
1 megabyte (MB) = 1024 x 1024 bytes = 1,024 KB = 220 bytes = 1,048,576 bytes 1 gigabyte (GB) = 1,024 MB = 230 bytes = 1,073,741,824 bytes
1 terabyte (TB) = 1024 GB = 240 bytes = 1,099,511,627,776 bytes
• the following units are used when expressing data rates (eg. Mb/s – note the lowercase b)
1 kilobit (Kb/s) = 1,000 bits per second
1 megabit (Mb/s) = 1,000 kilobits = 1,000,000 bits per second
• IEC prefixes KiB, MiB, GiB, … used to differentiate between 1000 and 1024
technically 1KB = 1000 bytes and 1KiB = 1024bytes (although KB is often used
17
to mean 1024)
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 18-Sep-18
BINARY NUMBERS
Programmer Calculator
• many calculators have a programmer mode (eg. Windows 10 calculator) for performing binary and hexadecimal arithmetic
• don’t use one until you know how to do the calculations “by hand”
• calculators NOT allowed in the CS1021 mid-term test or exams
CS1021 © 2018 jones@scss.tcd.ie School of Computer Science and Statistics, Trinity College Dublin 18-Sep-18
18