Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 24

1007ICT Introduction to

Computer Systems & Networks

Data Representation
Computer Data
Note: Data is plural, datum is singular

 Data is basically any type of information that can be


stored in a computer memory and processed.
 All kinds of information can be stored as long as it can
somehow be represented using 1’s and 0’s (bits).
 Some examples of different data types
 Texts / characters 01000001 = letter “A”
 Different types of numbers 00000011 = Number 3
 Data Processing Instructions 00011001 = Instruction “Add”
 Audio and Images etc 11111111 = White Pixel
 Since each of these is stored as bits, how those bits are
determined depends on what type of data the computer
expects or thinks they are.
Integer Representation

 An integer is a whole number (not a real number)


 Integers can be represented in binary form by writing it
in base 2 instead of our common base 10 notation.
 In base 10 you count digits 0..9, then powers of 10, eg 10, 100
 In base 2 you count digits 0..1, then powers of 2, eg, 10(=2), 100(=4)

 Any number can be evaluated using this formula


n
  di  b i 1 x 100 = 100
+2x 10 = 20
 Where: i 0
+3x 1= 3
 d subscript i is the i-th digit
123
 b superscript i is the base (or radix) raised to the ith power
 and the number is the sum of terms from i = 0 to i = n
 •and n is the number of digits in the number -1
Binary Integers
 The place of a digit in a binary number gives the
power of 2 that the value of the digit represents
 The value of a binary number can be read as follows
Bit Position 7 6 5 4 3 2 1 0
Bit Value 1 1 0 0 0 1 0 1

Number = 1*27 + 1*26 + 0*25 + 0*24 + 0*23 + 1*22 + 0*21 + 1*20


= 1*128 + 1*64 + 0*32 + 0*16 + 0*8 + 1*4 + 0*2 + 1*1

 Examples: Binary to Decimal Conversion


 000000112 = 2 + 1 = 3
 000100102 = 16 + 2 = 18
 100001012 = 128 + 4 + 1 = 133
 111100002 = 128 + 64 + 32 + 16 = 240
Powers in Different Bases


Place
Decimal Binary Hex / Octal
100 1 20 1 160 1
101 10 21 2 161 16
102 100 22 4 162 256
103 1,000 23 8 163 4096
104 10,000 24 16 164 65,536
105 100,000 25 32
106 1,000,000 26 64 80 1
107 10,000,000 27 128 81 8
108 100,000,000 28 256 82 64
109 1,000,000,000 29 512 83 512
1010 10,000,000,000 210 1024 84 4096
Integer Wordsizes

 If we have a binary word of N bits we can


represent 2N different values with those N bits.
Wordsize No. Values Range
1 2 0 .. 1
2 4 0 .. 3
3 8 0 .. 7
4 16 0 .. 15
8 256 0 .. 255
16 65,536 0 .. 65,535
32 4,294,967,296 0.. 4,294,967,295
N 2N 0 .. 2N - 1
Decimal to Binary Conversion
 To convert any number N, base 10, to binary:
1. Find the largest power of 2 <= N, say 2X , then subtract 2X from N.
2. Substitute N- 2X for N, then repeat step 1 until N becomes 0.
3. Assemble the binary number with 1s in the bit positions
corresponding to powers of 2 used in the decomposition of N, and
0s elsewhere.
 Example Convert 770 to binary :
 Step 1a) The largest power of 2 <= 770, is 29 or 512.
 Step 1b) Subtract 512 from 770 which gives you 258.
 Step 2a) The largest power of 2 <= 258, is 28 or 256.
 Step 2b) Subtract 256 from 258 which gives you 2.
 Step 3a) The largest power of 2 <= 2, is 21 or 2.
 Step 3b) Subtract 2 from 2 which gives you 0. Stop finding powers of 2.
 Step 4a) Powers of 2 used to decompose 770 are: 9, 8, and 1.
 Step 4b) Set those bit positions to 1
9 8 7 6 5 4 3 2 1 0
1 1 0 0 0 0 0 0 1 0
Alternate Decimal to Binary
 Recursively divide the decimal number by 2, noting the
remainder each time (which will be either 0 or 1).
 When you hit 0, write the remainders in reverse for the answer
 Convert 71010 to binary:
 710 / 2 = 355, remainder 0
 355 / 2 = 177, remainder 1
 177 / 2 = 88, remainder 1
 88 / 2 = 44, remainder 0 Rotate
 44 / 2 = 22, remainder 0 Column
 22 / 2 = 11, remainder 0
 11 / 2 = 5, remainder 1
 5 / 2 = 2, remainder 1
 2 / 2 = 1, remainder 0
 1 / 2 = 0, remainder 1
 Putting the remainders together (in reverse order) gives:
 71010 = 10110001102
Hexadecimal
 Hexadecimal and binary numbers are closely related
since (16 is a power of 2. ie, 24 ), conversion between
hexadecimal and binary is also straightforward.
 Hexadecimal has 15 digits (from 0 to F):
Dec Bin Hex Dec Bin Hex Dec Bin Hex Dec Bin Hex
0 0000 0 4 0100 4 8 1000 8 12 1100 C
1 0001 1 5 0101 5 9 1001 9 13 1101 D
2 0010 2 6 0110 6 10 1010 A 14 1110 E
3 0011 3 7 0111 7 11 1011 B 15 1111 F

 Since each Hex digit only requires 4 bits to represent it


is easy to convert between binary and hexadecimal by
grouping lots of four binary digits together.
 Hex numbers often have a “0x” in front to identify them
Octal
 Octal numbers are similar to hexadecimal but
only has 8 digits (from 0 to 7)
 Octal digits are made up from groups of 3 bits
Dec Bin Octal Dec Bin Octal Dec Bin Octal
0 000 0 8 1 000 10 16 10 000 20
1 001 1 9 1 001 11 17 10 001 21
2 010 2 10 1 010 12 18 10 010 22
3 011 3 11 1 011 13 19 10 011 23
4 100 4 12 1 100 14 20 10 100 24
5 101 5 13 1 101 15 21 10 101 25
6 110 6 14 1 110 16 22 10 110 26
7 111 7 15 1 111 17 23 10 111 27
Binary Addition
 How do we add up binary numbers?
 We add up each binary place and take any
carry over into the next column.
 EG: Single Bit Addition
0 0 1 1 A
0 1 0 1 B+
0 1 1 10

 EG: Multiple Bit Addition


0010 2 0111 7 0011 3 1001 9 A
0100 4 1100 12 0111 7 0110 6 B+
0110 6 10011 19 1010 10 1111 15
Negative Integers
 So far we have only dealt with positive integers.
 We need some way of representing the signs (+ or -).
 We could try making the most significant bit in a word
0 for positive numbers and 1 for negative ones. E.g.,
 +2 = 0 0000010 +127 = 0 1111111 +0 = 0 0000000
 -2 = 1 0000010 -127 = 1 1111111 -0 = 1 0000000
 Lets try to adding +7 to –5 with binary addition Hm
Sign Bit m

+7 0 0000111 Sign
0 = +ve
-5 1 0000101 1 = -ve
+2 1 0001100 = -12!
 This does not work we need some other way of
representing negative numbers so addition works
One’s Complement
 Using a sign bit plus binary magnitude does not work.
 Lets try using a sign bit plus the bitwise complement
of the magnitude.
 +2 = 0 0000010 +127 = 0 1111111 +0 = 0 0000000
 -2 = 1 1111101 -127 = 1 0000000 -0 = 1 1111111

 Now lets try to add some numbers like before Hm


m
-4 1 1111011 -3 1 1111100
+6 0 0000110 -2 1 1111101
+2 (1)0 0000001 = +1! -5 (1)1 1111001 = -6!

Carry Bit (ignored)

 Still wrong but closer – we are only out by 1.


 The clue to what is wrong is due to having 2 zeros
Two’s Complement
 Lets try one’s complement +1 for negative numbers.
 +6 = 0 0000110 +2 = 0 0000010 -2 = 1 1111110
 +5 = 0 0000101 +1 = 0 0000001 -3 = 1 1111101
 +4 = 0 0000100 0 = 0 0000000 -4 = 1 1111100
 +3 = 0 0000011 -1 = 1 1111111 -5 = 1 1111011
 Now lets try to add some numbers like before
+7 0 0000111 -3 1 1111101
-5 1 1111011 -2 1 1111110
+2 (1)0 0000010 = +2 -5 (1)1 1111011 = -5
+3 0 0000011
-5 1 1111011 This works!
-2 (1)1 1111010 = -2
 We can use two’s complement with binary addition
Number Representations
 Numbers are stored as a word sized set of bits.
 It is possible to use both smaller and larger words
sizes to represent numbers of different ranges.
 Some programming languages (like C) might use the
following unsigned data types on a 32 bit word size
architecture:
Type Bits Range
BYTE 8 0 .. 255
WORD 16 0 .. 65536
DWORD 32 0 .. 4,294,967,296
char 8 -128.. +127
short INT 16 -32767 ..+32766
long INT 32 -2147483647 .. +2147483646
I/O Data Types and Encoding
 I/O devices need to represent a wide
range of different data types
 Keyboard presses
 On screen text display
 Bitmapped Images and Graphics
 Audio, Video
 A processor needs to be able to process
all of these different types
 Hence each of these data types has its
own internal binary representation
Character Data
 Keyboard input consists of a series of individual characters
 Onscreen text display also consists of characters
 Each character is represented using an 8 bit code that is
defined by the ASCII standard
 We can represent a string of characters using a string of small
binary words: “HELLO WORLD” = 48454C4C20574F524C44
Dec 65 66 67 68 69 70 71 72 73 74
Hex 41 42 43 44 45 46 47 48 49 4A
Char A B C D E F G H I J

Dec 97 98 99 100 101 102 103 104 105 106


Hex 61 62 63 64 65 66 67 68 69 6A
Char a b c d e f g h i j

Dec 48 49 50 51 52 53 54 55 56 57
Hex 30 31 32 33 34 35 36 37 38 39
Char 0 1 2 3 4 5 6 7 8 9
ASCII Table
HEX MSD 0 1 2 3 4 5 6 7
LSD BITS 0000 0001 0010 0011 0100 0101 0110 0111
0 0000 NUL DLE space 0 @ P ` p
1 0001 SOH DC1 ! 1 A Q a q
2 0010 STX DC2 “ 2 B R b r
3 0011 ETX DC3 # 3 C S c s
4 0100 EOT DC4 $ 4 D T d t
5 0101 ENQ NAK % 5 E U e u
6 0110 ACK SYN & 6 F V f v
7 0111 BEL ‘ 7 G W g w
8 1000 BS ( 8 H X h x
9 1001 TAB ) 9 I Y i y
A 1010 LF * : J Z j z
B 1011 VT ESC + ; K [ k {
C 1100 FF , < L \ l |
D 1101 CR - = M [ m }
E 1110 SO . > N ^ n ~
F 1111 SI / ? O _ o
Character Data
 There are other standards for representing characters.
 EBCDIC
 The EBCDIC (Extended Binary Coded Decimal Interchange Code) is
another 8 bit format introduced by IBM. At last count there were
about six different (incompatible) versions of the EBCDIC in use.
 EBCDIC is only used in some older IBM mainframes.
 Unicode
 Unicode uses different sized codewords to represent many 1000’s of
different characters to support a wide number of foreign languages.
 The first 128 characters in Unicode are identical to ASCII.
 There are several mapping (char to number) methods specified in
Unicode: UTF-32, UTF-16, UTF-8, and UTF-7. The numbers indicate
the number of bits in one codeword. In some cases a variable
number of codewords can be used to represent each character.
 Unicode is used in operating systems such as Windows 2000/XP,
and in some compilers such as Java (a char in Java is 16 bits).
Bitmap Images
 A bitmap image is just a 2D array of unsigned numbers
where each element specifies the colour at that location.
 Each individual element in the array is called a pixel
 A computer display is basically just a large bitmap
 The number of bits used to represent each pixel defines the
number of colour shades that the bitmap can contain
1 bit Image 4 bit Image
12 x 10 pixels 12 x 10 pixels

1 1 1 1 1 1 1 1 1 1 1 1 F F F F F F F F F F F F
1 1 1 0 1 1 1 0 0 0 0 1 F F F C F F F 0 0 3 6 F
1 1 1 0 1 1 1 0 0 0 0 1 F F F B F F F 0 3 6 6 F
1 = white
0 = black

F = white
0 = black
1 1 1 0 1 1 1 0 0 0 0 1 F F 8 A A F F 3 6 6 9 F
1 1 0 0 0 1 1 0 0 0 0 1 F F 6 8 8 F F 6 6 9 C F
1 1 0 0 0 1 1 0 0 0 0 1 F F 4 6 6 F F 6 9 C C F
1 0 0 0 0 0 1 1 1 1 1 1 F 2 3 4 4 4 F F F F F F
1 0 0 1 0 0 1 1 1 1 1 1 F 1 1 F 2 2 F F F F F F
1 0 0 1 0 0 1 1 1 1 1 1 F 0 0 F 0 0 F F F F F F
1 1 1 1 1 1 1 1 1 1 1 1 F F F F F F F F F F F F

12 bits wide 48 bits wide


Colour Formats - 1
 Colour bitmaps can be represented in a various ways
depending on the colour resolution of the output device
 The bit depth (N) determines the number of colours that can
be simultaneously displayed on the screen = 2N
 Bitmaps – use a single bit to represent each pixel (ON / OFF).
 GreyScale – uses up to 8 bits/pixel to generate output from
black (0) to white (255).
 Colour mapped – uses 8 bits for each pixel as an index into a
24-bit colour (RGB) lookup table to display up to 255 colours.
 True Colour – uses 24-bits / pixel to represent the 3 primary
colour components (R,G,B) using 8 bits each. May use 5 bits
per component (15/16 bit colour).
 Black(0,0,0), White (255,255,255), Red(255,0,0), Blue(0,0,255) etc
 Some formats (MSWindows) use BGR instead of RGB
 Some formats use 32bits to support an additional alpha channel that
specifies transparency for each pixel as well as the RGB colour.
Colour Formats - 2
 In a true colour image the colour
planes / components are normally
interleaved but can also be stored
sequentially.
 Colour-mapped displays permit a
reduced set of colours to be
selected for display from a much
larger palette.
Byte 0 1 2 3 4 5 6 7 8 9 10…
Interleaved RGB RGB RGB RGB
pel 1 pel 2 pel 3 pel 4

RRRRR…,GGGGG,…BBBBB
Sequential
pel 1 pel 4

Lookup Table RGB


RGB
RGB
Data Bytes : 8 5 1 7 5 2 6 3 9…. RGB
Audio and Sounds
 Waveform Audio is simply a digitised audio sound wave.
 Analogue audio is captured using a microphone and
converted to digital in the sound card and stored as PCM.
 Analogue to Digital conversion first takes discrete samples or
snapshots of the value of the sound wave in time and then
quantises (or converts) the value of each sample into an
unsigned number using a determined wordsize.
 The resulting representation is an array of unsigned words
and is called Pulse Code Modulation (PCM)
 8-bit Example: 80 60 40 40 20 20 20 40 40 60 80 A0 C0 CO E0 E0 E0
E0
C0
A0
80
60
quantised
Sound wave sampled 40
20
Sample Exam Questions

 What does each  What are three


place represent in a: commonly used
 decimal number representations for
 binary number character- (text-)
 octal number based data?
 hexadecimal number
 What is the most
 Show the one’s & basic representation
two’s complement for image data?
equivalents of
 56 – 125
 What is the basic
representation for
audio data?

You might also like