02-Data Representation in The Computer Systems

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 42

COMP502

Foundation of IT Infrastructure
Week 02 - Data Representation 1
What is covered today?

• Data in computers are all


• Binary
• We will learn conversion between
• Decimal (base 10)
• Binary (base 2)
• Octal (base 8)
• Hexadecimal (base 16)

• Basic binary calculations


Opening question

 How do we talk the computer language?


 Easy, 010010011010101001000100011101..
 What?
Data Representation
• How do we represent information using electrical
signals?
• Store data:
• Binary, Octal, Decimal
• Signed Numbers

• Performing Arithmetic:
• Addition, Subtraction
• Shifting

• Save types and representation:


• Integer
• Floating point -- IEEE format
Data in a computer
• At the lowest level, a computer is an
electronic machine.
• works by controlling the flow of
electrons
• Easy to recognize two conditions:
• presence of a voltage – we’ll call this state
“1”
• absence of a voltage – we’ll call this state
“0”
• Could base state on value of voltage, but
control and detection circuits more complex.
• compare turning on a light switch to
measuring or regulating voltage
Computer is a Binary Digital System.

• Basic unit of information is the binary digit, or bit.


• Values with more than two states require multiple bits.
• A collection of two bits has four possible states:
00, 01, 10, 11
• A collection of three bits has eight possible states:
000, 001, 010, 011, 100, 101, 110, 111
• A collection of n bits has 2n possible states.
What kinds of data to represent?

• Numbers – signed, unsigned, integers, floating point,


complex, rational, irrational, …

• Text – characters, strings, …

• Images – pixels, colors, shapes, …

• Sound – music, melody, ringtones, …

• Logical – true, false

• Instructions – commands

• …We’ll start with numbers…


Data Types in Computer Systems
Unsigned Integers (No negatives)
• Non-positional notation
• could represent a number (“5”) with a string of ones (“11111”)
• problems?
• Weighted positional notation
• like decimal numbers: “329”
• “3” is worth 300, because of its position, while “9” is only worth 9
most least
329 significant
101 significant

102 101 100


22 21 20
3x100 + 2x10 + 9x1 = 329 1x4 + 0x2 + 1x1 = 5
Polynomial Evaluation
• With Fractional Part (Radix = 10):
36.7210 = 3  101 + 6  100 + 7  10-1 + 2  10-2

• With Fractional Part (Radix = 2):


10.012 = 1  21 + 0  20 + 0  2-1 + 1  2-2
=?

• General Case (Radix = R):


(S1S0.S-1S-2)R =
S1  R1 + S0  R0 + S-1  R -1 + S-2  R-2
Unsigned Integers (cont.)
•  An n-bit unsigned integer represents any of 2n
(integer) values:
from 0 to
22 21 20 Value

• 32 bit integer represents: 0 0 0 0


0 0 1 1
0 1 0 2
• 0 to 4294967295
0 1 1 3
• 64 bit integer represents: 1 0 0 4
1 0 1 5
• 0 to 1.8 * 10^19 1 1 0 6
1 1 1 7
Binary to Decimal Conversion

• Converting to decimal, so we can use polynomial evaluation:

1 0 1 1 0 1 0 1 2

127 + 026 + 125 + 124 + 023 + 122 + 021 + 120

= 128 + 0 + 32 + 16 + 0 + 4 + 0 + 1
= 18110
• https://www.mathwarehouse.com/programming/binary-nu
mbers-game/
Power of 2 to remember!
Decimal to Binary Conversion
 Divide by target radix (2
in this case)
 Remainders become digits in
the new representation
(0 <= digit < R)
 Digits produced in right to left
order.
 Quotient is used as next
dividend.
 Stop when the quotient
becomes zero, but use the
corresponding remainder.
Decimal to Binary Conversion

 97  2  quotient = 48, remainder = 1 (LSB)


48  2  quotient = 24, remainder = 0
24  2  quotient = 12, remainder = 0
12  2  quotient = 6, remainder = 0
62 quotient = 3, remainder = 0
32 quotient = 1, remainder = 1
12 quotient = 0 (Stop) remainder = 1 (MSB)

 Result = 1 1 0 0 0 0 12
Worksheet

• Give me a number in decimal, I’ll turn it to binary.


Converting Binary to Octal
start
100011001010012
STEP ONE: Take the binary number and
from right to left, group all placeholders in
triplets (3).
Add leading zeros, if necessary:

010 001 100 101 001


Converting Binary to Octal

STEP TWO: Convert each triplet to its


single-digit octal equivalent. (HINT: For
each triplet, the octal conversion is the
same as converting to a decimal number):
010 001 100 101 001
2 1 4 5 1
010
0 001 100 101 0012 = 214518
Convert Octal to Binary

STEP ONE: Take each octal digit and


convert each digit to a binary triplet. Keep
leading zeros:
STEP Two: Convert each group from
binary to decimal
4 3 5 2 0
100 011
0 101 010
0 000

435208 = 1000111010100002
Hexadecimal number system

• To shorten binary, they also use Hexadecimal.


• Each 4 digits in binary is equivalent to 1 in hex
• Hexadecimal generally is used for colors
Hexadecimal numbers

• There are new symbols for the Base-16 equivalents of the


Base-10 numbers: 10, 11, 12, 13, 14 and 15. We have:

DEC 0 1 2 3 4 5 6 7

HEX 0 1 2 3 4 5 6 7

DEC 8 9 10 11 12 13 14 15

HEX 8 9 A B C D E F
Decimal Hexadecimal Octal Binary
0 0 0 0000
1 1 1 0001
2 2 2 0010
3 3 3 0011
4 4 4 0100
5 5 5 0101
6 6 6 0110
7 7 7 0111
8 8 10 1000
9 9 11 1001
Decimal Hexadecimal Octal Binary

10 A 12 1010

11 B 13 1011

12 C 14 1100

13 D 15 1101

14 E 16 1110

15 F 17 1111
Converting Hex to Dec

• To convert from Hex to Decimal,


• Multiply the value in each position by its hex
weight and add each value.

• Use the expansion form


• 0xAFB1
• = 10x163 +15x162 +11x161 + 1x160
• = 4497710
Decimal to Hex Conversion
• Repeated Division By 16
• Divide the decimal number by 16, and write the
remainder on the side as the least significant digit.

• This process is continued by dividing the quotient by 16


and writing the remainder until the quotient is 0.

• The remainders represent the hex equivalent of the


decimal number are written beginning at the least
significant digit (right) and each new digit is written to the
next more significant digit (the left) of the previous digit.

• Or, you can also convert Decimal to Binary, and then


from Binary to Hex
Example convert 196710 to Hex

Division Quotient Remainder Hex Number

1967/16 122 15 F

122/16 7 10 AF

7/16 0 7 7AF

Then (1967)10 = (7AF)16


Converting Hex to Bin
• Convert a hexadecimal number to a
binary number
• STEP 1: simply divided the binary number into 4-bit groups
• STEP 2: substitute the corresponding four bits in binary for
each hexadecimal digit in the number.

• For example, convert 0xABCD to a binary value, The


binary equivalent is:
• 0xABCD= 1010 1011 1100 1101
• Or 0xAFC7 = 1010 1111 1110 0111
Converting Bin to Hex

• Binary to Hex Conversion


• STEP 1: Break the binary number into 4-bit
groups from the right to the left.
• STEP 2: Convert the 4-bit binary number to its
Hex equivalent.

• For example:
• the binary value 1010111110110010 will be written
as:
• 1010 1111 1011 0010 = 0xAFB2
Binary Addition

+ 0 1
 0+0=0
 0+1=1
 1+0=1 0 0 1
 1 + 1 = 10
1 1 10
Multiply Binary numbers
* 0 1
 0*0=0
 0*1=0

0 0 0
1*0=0
 1*1=1
1 0 1
Additions with Binary numbers

111111 carry 1 1 1 (carry)


• 111101 01101
100111+ 1 0 1 1 1+
-------------
• 1100100
100100
Multiply 1011 and 1010

1 0 1 1
× 1 0 1 0
---------------
0 0 0 0
1 0 1 1
0 0 0 0
1 0 1 1
------------------
1 1 01 1 1 0
Binary multiplication example

1 1 0 1 1 1
x 1 1 1
1 1 0 1 1 1
1 1 0 1 1 1
1 1 0 1 1 1
------------------------------------
1 1 0 0 0 0 0 0 1
ASCII Representation of Characters
Representation Interpretation

00100100
ASCII Code
$
Representing Text
• American Standard Code for Information
Interchange (ASCII)
• Developed from telegraph codes, alternative to IBM’s
Extended Binary Coded Decimal Interchange Code
(EBCDIC) in 1960s
• Printable and non-printable (ESC, DEL, …) characters
(127)
• Limited set of characters – many character missing,
especially language-specific
• Unicode – more than 110,000 characters covering 100
scripts
• UTF-8 is the form of Unicode which preserves the ASCII
encoding
Text: ASCII Characters
• ASCII: Maps 128 characters to 7-bit code.
00 nul 10 dle 20 sp 30 0 40 @ 50 P 60 ` 70 p
01 soh 11 dc1 21 ! 31 1 41 A 51 Q 61 a 71 q
02 stx 12 dc2 22 " 32 2 42 B 52 R 62 b 72 r
03 etx 13 dc3 23 # 33 3 43 C 53 S 63 c 73 s
04 eot 14 dc4 24 $ 34 4 44 D 54 T 64 d 74 t
05 enq 15 nak 25 % 35 5 45 E 55 U 65 e 75 u
06 ack 16 syn 26 & 36 6 46 F 56 V 66 f 76 v
07 bel 17 etb 27 ' 37 7 47 G 57 W 67 g 77 w
08 bs 18 can 28 ( 38 8 48 H 58 X 68 h 78 x
09 ht 19 em 29 ) 39 9 49 I 59 Y 69 i 79 y
0a nl 1a sub 2a * 3a : 4a J 5a Z 6a j 7a z
0b vt 1b esc 2b + 3b ; 4b K 5b [ 6b k 7b {
0c np 1c fs 2c , 3c < 4c L 5c \ 6c l 7c |
0d cr 1d gs 2d - 3d = 4d M 5d ] 6d m 7d }
0e so 1e rs 2e . 3e > 4e N 5e ^ 6e n 7e ~
0f si 1f us 2f / 3f ? 4f O 5f _ 6f o 7f del
ASCII Representation of Strings

48 65 6C 6C 6F 00 Computer uses
a terminating
H e l l o “NUL” byte of
all zeros at the
end of the
string.

41 75 53 4D 20 69 73 20 22 66 75 6E 22 21 00
A u S M i s “ f u n ” ! \0
How does computer store pictures
• It stores pixels and pixels.

Zoom

 0 to 255 for grey


 (A)RGB for coloured
images
 8 bits, 24 bits, 32 bits,
etc…
GIF vs JPG vs PNG

 JPG vs GIF

 PNG vs JPG vs GIF

• Size: RAW >> PNG >> JPG >> GIF


• JPG is lossy compressed images
• GIF only has 256 colours.
How does computer store sounds
• Sound is analogue information, it
is continuous sinusoidal waveform
• Amplitude (height): loudness
• Frequency (1/period) : pitch
• Humans can hear sounds in range
20-20,000 Hz

• Record amplitude at points and


turn to binary.
How does computer store sounds

You might also like