Topic 2: Data Flashcards
Why Binary?
Computers contain billions of transistors. These act as switches that have two states: on and off. These states are then represented in binary by 1(on) or 0(off). These are called bits.
Binary naming
Bits ( 1 bit)
Nibble ( 4 bits)
Byte (8 bits)
An arithmetic expression to show that 256 different colours can be represented in 8 bits
2¡(nr of bits) –> 2¡8
How to convert 6 to binary
0 1 1 0
Convert 78 into binary
0 1 0 0 1 1 1 0
1101 1001 into denary
128—64—-32—-16—–8—–4—–2—–1
——————————————————
1——1—–0——1——1—–0—–0—–1
——————————————————
128 + 64 + 6 + 8 + 1 = 217
Two’s complement signed integers
-Remove the - sign
-Convert the number to binary
-Flip the bits
-Add 1
How to represent -4 in binary
0 1 0 0 (convert +4 to binary)
1 0 1 1 (flip the bits)
1 1 0 0 (add 1)
Binary addition
0 + 0 = 0
0 + 1 = 1
1 + 1 = 0 and carry 1
Logical shift left by 2 places: 0001 0100
0101 0000
Logical shift right by 3 places: 1011 1000
0001 0111
Arithemtic shift right by 1 place: 1000 1000
1100 0100
Overflow
when an operation produces a result which requires more bits than available to store result
Consequences of overflow
Programs may crash or produce unreliable or incorrect results
1 reason why the result of adding 16 bit patterns must be 16 bit in length
The registers inside the machine that hold the original patterns are fixed in width, and so no more that 16 bits can be held.
Hexadecimal (10-16)
A-F
Convert C3 to binary
1) Each hexadecimal digit is converted to denary (C = 12) and 3)
2) Each denary number is converted into a nibble (1100)(0011)
3) The nibbles are combined to five the binary number (1100 0011)
ASCII in python
ord() returns the ASCII code in denary (“c”)–> 99
chr() returns the character for a denary code 100–> (“d”)
Upper case characters
65-90
Lower case characters
97-122
Digits 0 to 9
48-57