Data Representation (Copy)
Number Systems
- Denary number systems use base 10 system. The common numbers that we use.
- Binary number system
- Base 2
- It means different powers of 2
- Remember it starts with 2 to the power of 0
- Which means the first value is 1
- Anything to the power of zero is 1
- Bits
- Comes for Binary DigITS
- One bit is represented by one binary number
- 8 such bits is a byte
- Binary numbers look like 10110110
- You need to create a number line to convert binary to denary
2¹ = 2
2² = 4
2³ = 8
Basically
- Every number can be expressed in a binary number
- By putting a 1 for the 2 base that is required to make it and putting a zero to the 2 base that is not required to make it.
For example
| 128 | 64 | 32 | 16 | 8 | 4 | 2 |
So how to convert a number to a binary number.
Say you need to convert 87 to the binary form.
First select the highest 2 base number that is just smaller than it and put a 1 there.
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | |
| 1 |
The largest 2 base number which is smaller than 87 is 64 so put a 1 below 64. Now we need 87-64 = 23 more. Which means that the largest 2 base number that is below 23 is 16. So we put a one at 16
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | |
| 1 | 1 |
Now we need 23-16 = 7. Again the largest number below 7 is 4 so we put 1 at 4
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | |
| 1 | 1 | 1 |
Now we need 3 so it would be 2 and 1
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | |
| 1 | 1 | 1 | 1 |
Now put 0 in all other numbers.
- HINT: It does not matter if there is a zero or not on the left side of the largest number used. So here for example, if we do not put zero in 128, it does not matter. Any zeros to the left of the largest number used don’t matter as they don’t change the value, Any zero on the right matters, at the same time any 1 any where changes the value so watch out.
- Most students forget that first base number of 2 is 1 as 2 to the power of 0 is 1, and thus start the number line with 2 which changes everything.
- Don’t forget any zeros on the right, as value would change.
- Number line can go on to any power of 2, it is not necessary to stop at 128. Usually we stop here because these numbers add up to 255, which is the complete code of a byte.
So now
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | |
| 1 | 0 | 1 | 0 | 0 | 1 | 1 |
87 is binary form is 1010011. This is how you convert a denary number to binary number. If you have to convert a number from binary to denary, the same process will be used in reverse.
For example convert 10001110 to denary.
So this would be
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | |
| 1 | 0 | 0 | 0 | 1 | 1 | 1 | 0 |
So just add the numbers where there is 1
128 + 8 + 4 + 2 = 142.
So the number 10001110 (binary) in denary form in 142.
Hexadecimal
A binary digit is equal to one BIT (up to 255 maximum).
- A collection of 8 bits together is a byte which is the smallest unit of computer memory.
- In a 16-bit system, 1 byte is equal to 16 bits rather than 8.
- Any system always have bits in bytes which are multiples of 8.
- Thus 1 Kilobyte is basically 210 bytes, while one megabyte is 220 bytes and so on.
- It has been simplified to 1 Kb = 1000 bytes, 1 MB = 1000 KB etc.
However when measuring RAM, kibibytes, mebibytes etc are used where the old binary method is used. Here in this course the term kilobyte and kibibyte shall be the same with the binary system being used for both.
Binary numbers can be used to describe the functionality of software etc. For example if a 1 for 25 means lights on and 0 means lights off, while a 1 for for 21 means fans on and 0 means off, then 100010 means lights and fans on while 100000 means lights on but fans off which is both binary number format here.
The other common system that is followed is the hexadecimal system, where the base is 16. 1 hexadecimal value has 4 binary values. So basically the 16 digits are
| Hexadecimal Numberer | Denary Value | Binary Code |
| 0 | 0 | 0000 |
| 1 | 1 | 0001 |
| 2 | 2 | 0010 |
| 3 | 3 | 0011 |
| 4 | 4 | 0100 |
| 5 | 5 | 0101 |
| 6 | 6 | 0110 |
| 7 | 7 | 0111 |
| 8 | 8 | 1000 |
| 9 | 9 | 1001 |
| A | 10 | 1010 |
| B | 11 | 1011 |
| C | 12 | 1100 |
| D | 13 | 1101 |
| E | 14 | 1110 |
| F | 15 | 1111 |
How to convert a binary number into hexadecimal
- First divide the binary number into block of 4 STARTING FROM THE RIGHT
- For example if the number is 1100011110
- Then the blocks will be, 1110, 0001, 0011
- Remember, blocks are selected from right not values. Secondly, if the last block is short of numbers, as here we had only 11, then add zeros to complete the 4 on the right (Zeros on right dont change values).
- Now convert each block in hexadecimal
- 1110 become E
- 0001 becomes 1
- 0011 becomes 3
- Put back together
- The hexadecimal value becomes 31E
How to convert from hexadecimal to binary.
- Take each number separately. For example for a hexadecimal number F6B
- The 3 numbers would be F, 6, B
- Convert each in binary
- F is 15 which is 1111
- 6 is 0110
- B is 11 which is 1011
- Now put back
- 111101101011
Binary Addition and Subtraction
- Two methods to represent positive and negative numbers
- One’s complement
- Invert each binary digit.
- For example 1 becomes zero and zero becomes 1.
- Therefore 10011100 which represents the number 156.
- Therefore, in one’s complement form 01100011 is the representation for -156.
- Two’s complement form
- First, we invert the digits like one’s complement.
- Then we add another 1 to the right most bit
- Therefore, 10011100 represents 156.
- First invert it: 01100011 is step 1
- Step 2: 01100100
- (This addition explained below)
- If you need to convert a 2’s complement number back to the denary form, it would be done as follows
- The new number line we will use will not be 128, 64, 32, 16, 8, 4, 2, 1
- Instead, it will be -128, 64, 16, 8, 4, 2, 1
- Now you can just put the two’s complement form of binary and just convert it back to denary the old fashioned way on the new number line mentioned above.
- One’s complement
Binary Addition and Subtraction
- Binary addition is simple
- Just like simple addition when it comes 10 we take a carry forward
- We will take a carry forward when it becomes 2.
- Let’s for example we add 11001101 and 10110001
| 1 | 1 | 0 | 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 1 | 1 | 0 | 0 | 0 | 1 |
| It becomes 2 so we make it 0 and carry 1 | |||||||
| 10 | 1 | 1 | 1 | 1 | 1 | 1 | 0 |
- Now we have 9 bits, so we ignore the left most bit
Hexadecimal Number System
- Memory Dump usage
- When the memory contents are output to monitor of printer
- They can be easy to look at and locate the error
- More manageable than binary
- BCD
- Binary-coded decimal
- 4 bit system to represent a denary digit
- 0000 =1
- 0001=1
- 0010=2
- 0011=3
- 0100=4
- 0101=5
- 0110=6
- 0111=7
- 1000=8
- 1001=9
- We can form bytes to find the numbers.
- In a single byte we can represent 3 as
- 00000011
- In case of 2 bytes
- We can represent 3 and 5 as
- 00110101
- Binary-coded decimal
- BCD is used to for representation on calculators
Character Sets – ASCII Code and Unicode
- ASCII code
- American Standard Code for Information Interchange
- Set up in 1963
- Newer version in 1986
- The standard character set consists of 7-bit codes
- 0 to 127 in denary.
- 00 to 7F in hexadecimal
- These codes represent different numbers, letters and characters that are found on the standard keyboard
- 32 codes are control codes (0 to 31 in the denary system and 00 to 19 in the hexadecimal system)
- Between lowercase and uppercase codes for the same character, the sixth bit changes from 1 to 0.
- 0 represents uppercase
- 1 represents lowercase
- Character sets are grouped together in sequences
- This makes the usability faster
- Extended ASCII uses 8-bit codes
- 0 to 255 in denary
- 0 to FF in hexadecimal
- It provides another 128 codes that allow characters for the use of non-English alphabets and graphical characters as well.
- American Standard Code for Information Interchange
- Disadvantages
- Does not represent non-Western languages
- Different methods of coding have existed between DOS and Windows over the years.
- One coding system is called Unicode
- Represents all languages around the world.
- Supports many operating systems, search engines and internet browsers that are used globally
- It overlaps with standard ASCII as the first 128 characters are the same.
- Unicode can support several thousand characters in total.
- ASCII uses one byte to represent a character
- Unicode uses 4 bytes per character
- The Unicode Consortium was set up in 1991.
- Version 1.0 published with five goals
- Create a universal set covering all languages and writing systems
- Be more efficient than ASCII
- Unambiguous encoding system where both 16-bit and 32-bit values represent the same characters.
- Uniform encoding with each character coded as 16-bit or 32-bit code
- Reserve part of the code for private use.
- User can assign codes for own characters and symbols
- Version 1.0 published with five goals
