Python Data Representation Notes.
Python Data Representation Notes.
The Binary System is a way of writing numbers using only the digits 0 and 1.
This is the method used by the (digital) computer.
How can we convert numbers from the binary system to the decimal system?
Follow this example. We have 10110011. Note that this number is a sequence of
8 bits, so it is a byte. Add the weights as seen in the diagram. Add all weights
that are associated with the bits that are equal to 1.
Weights 128 64 32 16 8 4 2 1
Number 1 0 0 1 0 1 1 0
The method we have seen here is called the positional notation method.
Exercise
a. 11001
b. 11011101
c. 1110010
Suppose we want to convert the decimal number 149 to binary. One way we can
do this is by repeatedly dividing the number by 2 and then taking the remainder
digits to get the solution. An example is shown here:
2 149
2 74 remainder 1
2 37 remainder 0
2 18 remainder 1
2 9 remainder 0
2 4 remainder 1
2 2 remainder 0
2 1 remainder 0
0 remainder 1
Quotient Remainder
after after
dividing by dividing by
2 2
After performing this process you have to read the remainders FROM BOTTOM TO
TOP. This gives us that the decimal number 149 is equal to the binary number
10010101.
Exercise
a. 108
b. 53
c. 74
Number Bases
Note that:
2725 = 2x1000 + 7x100 + 2x10 + 5.
2725 = 2x103 + 7x102 + 2x101 + 5x100.
Working on the same principles we can show that the base number of the binary
system is 2.
Note also that the base number shows us how many different symbols are
required to express a number i.e. in the decimal system we need 10 symbols (0, 1,
2, 3, 4, 5, 6, 7, 8 and 9) while in the binary system we need 2 (0 and 1).
Carry
First number 0 0 1 1 1 1 0
Second number 1 0 0 1 0 1 1 Step 1:
Result of addition 1 0 + 1 gives 1
Carry 1
First number 0 0 1 1 1 1 0
Second number 1 0 0 1 0 1 1
Result of addition 0 1
Step 2:
Carry 1 1 1 + 1 gives 0 carry 1
First number 0 0 1 1 1 1 0
Second number 1 0 0 1 0 1 1
Result of addition 0 0 1
Carry 1 1 1 Step 3:
First number 0 0 1 1 1 1 0 1 + 0 + 1 gives 0
Second number 1 0 0 1 0 1 1 carry 1
Result of addition 1 0 0 1
Add the following binary numbers. Then check your result by using decimal
numbers.
a. 1010001 and 0101110
b. 1011011 and 1011100
Borrow
First number 1 0 1 1 0 1 1 Step 1:
Second number 0 1 0 1 1 1 0 1 – 0 gives 1
Result of subtraction 1
Borrow
First number 1 0 1 1 0 1 1
Second number 0 1 0 1 1 1 0
Result of subtraction 0 1
Step 2:
Borrow 2 1 – 1 gives 0
First number 1 0 1 1 0 1 1
Second number 0 1 0 1 1 1 0
Result of subtraction 1 0 1
Step 3:
91 Proceeding in this way we get 0 - 1 means that we
have to borrow
46 Borrow 2 2 2
First number 1 0 1 1 0 1 1
Second number 0 1 0 1 1 1 0
45 Result of subtraction 0 1 0 1 1 0 1
Exercise in Subtraction
Perform the following binary subtractions. Then check your results by using
decimal numbers:
a. 10001101 – 00101100
b. 10000101 – 01010111
Hexadecimal Numbers
Hexadecimal numbers are based on the number 16. A hexadecimal number uses
16 symbols which are 0, 1, 2 … 8, 9, A, B, C, D, E and F. A stands for ten, B
stands for eleven, C stands for twelve etc. The term ‘hexadecimal’ is shortened to
‘hex’. Hex numbers are used as a shortcut notation for binary numbers. The
conversions from binary to hex and vice-versa are very simple and straightforward.
The following example shows how the decimal number 400 is converted to hex.
1. Break the binary number into 4-bit sections from the LSB (least significant
bit) to the MSB (most significant bit).
2. Convert the 4-bit binary number to its Hex equivalent.
8 A 1 D Multiply 13 by 1.
Multiply 8x16x16x16. This gives 13.
This gives 32768.
Multiply 1 by 16.
Multiply 10x16x16.
This gives 16.
This gives 2560.
Exercise
Consider three bits. There are eight different combinations of ones and zeros
(these are 000, 001, 010, 011, 100, 101, 110 and 111). These eight combinations
can be made to represent the range of numbers from 0 to 7 or the numbers from -
4 to 3. We say that the range “from 0 to 7” is unsigned and the range “from -4 to
3” is signed.
In the Sign and Magnitude representation the leftmost bit is reserved to represent
the sign. 1 indicates negative and 0 indicates positive. Let us take an example with
8 bits.
sign 64 32 16 8 4 2 1
0 1 0 0 1 1 0 1 7710
1 1 0 0 1 1 0 1 -7710
Now let us consider 4 bits. All the numbers are represented in the following table.
Binary Decimal
Sign & Magnitude
1111 -7
1110 -6
1101 -5
1100 -4
1011 -3
1010 -2
1001 -1
Note that the value zero is repeated twice. The range of values is between -7 and 7.
If we had one byte the minimum value would be 11111111 (-12710) and the
maximum would be 01111111 (12710).
Apart from the repeated zero another problem with sign and magnitude
representation is that it is not good for computation e.g. making an addition of two
numbers etc.
Two’s Complement
Suppose we have one byte to represent a number, say -8710, in two’s complement.
This would be done in the following steps:
(1) Write 8710 in binary. This gives 01010111.
(2) Change all ones to zeros and all zeros to ones. This gives
10101000 (this number is called one’s complement)
(3) Add 1. 10101000 + 1 gives 10101001.
Y Is N N
negative?
Add 1.
Stop
Exercise
Example 1
5710 = 001110012
Example 2
10410 = 011010002
-10410 = 100110002
Another example:
Exercise
Perform the following calculations using two’s complement arithmetic. All numbers
are memorized in one byte.
(a) 99 – 56, (b) 85 – 106, (c) -25 – 30
Suppose we know that the number 1011.01102 is unsigned, what is its conversion
in decimal?
In this representation (i.e. we have 8 bits and the decimal point is fixed after the
fourth bit; this is called fixed-point representation):
the maximum number is 1111.1111 (15.9375), and
the minimum number is 0000.0000
What we have seen above is the conversion of a binary number (with a fractional
part) into decimal representation. How do we do the opposite? Suppose we want to
convert 13.59375 to binary we execute the following steps:
1. Convert the whole part to binary i.e. 1310 = 10112
2. Convert the decimal part to binary. This is done using the algorithm shown
in the diagram below. So 0.5937510 = 0.100112 (read the last column from
top to bottom)
Exercise
(a) Convert the following binary numbers to decimal (i) 11001.0112, (ii)
10011.100112
(b) Convert the following decimal numbers to binary (i) 23.687510, (ii)
87.1562510
The following table shows some maximum and minimum values (on one byte).
Exercise
In each of the following questions find the maximum, minimum and the range of
the numbers. Assume that the numbers are made up of 6 bits.
Exercise
(a) Convert the following decimal numbers in 8421 BCD: (i) 3810, (ii) 10910.
(b) Convert the following BCD number in decimal: 0111 1001 0001.
Both ASCII and Unicode are codes that convert characters (like ‘n’, ‘N’, ‘@’, ‘+’ etc)
and control codes (like ‘backspace’ and ‘line feed’ which causes a printer to
advance its paper). ASCII is an acronym for the ‘American Standard Code for
Information Interchange’. ASCII is a code for representing English characters as
numbers, with each letter assigned a number from 0 to 127 (using 7 bits). For
example, the ASCII code for uppercase M is 77. Most computers use ASCII codes
to represent text, which makes it possible to transfer data from one computer to
another.
Text files stored in ASCII format are sometimes called ASCII files.
The standard ASCII character set uses just 7 bits for each character. There are
several larger character sets that use 8 bits, which gives them 128 additional
characters. The extra characters are used to represent non-English characters
(like ö, ū, etc), graphics symbols (like □, ◊, ●, etc), and mathematical symbols (like
⅛, √, ∫, etc). The DOS operating system uses a superset of ASCII called ‘extended
ASCII’ or ‘high ASCII’. Extended ASCII uses 8 bits to represent a character.
Another ASCII derivative is ‘ISO Latin 1’.
Unicode uses 16 bits, which means that it can represent more than 65,000 unique
characters. This is necessary so that other languages, such as Greek, Chinese and
Japanese can have their symbols represented.