Binary

advertisement
Computer Systems 1
Fundamentals of Computing
Data Representation
Decimal, Binary, Hexadecimal
Data Representation
Decimal (Base 10)
 Binary (Base 2)
 Hexadecimal (Base 16)

Computer Systems 1 (2004-2005)
Decimal



Base 10
Numbers we use in everyday life
10 values then increment to next digit position on left


0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Each position represents a value of 10, to the positions power



Units (100), Tens (101), Hundreds (102), etc...
‘Ten fingers & ten toes’
Decimal numbers

E.g.


5
64281
-3564
Computer Systems 1 (2004-2005)
Binary




Base 2
Commonly used in computers to represent data
Used for all calculations and logic test results
2 values, then increment next digit position on left


0 or 1
Each position represents a value of 2 to the positions power



20 (1), 21(2), 22 (4), 23 (8), 24 (16), etc…
On or off (Digital)
Binary numbers

E.g.



01
1010110
00000000
11111111
Computer Systems 1 (2004-2005)
Binary Bits ‘n’ Bytes

A bit is a single binary digit (1 or 0)
A kilobit (KB) is 1024 bits

A byte is eight bits






Four bits are sometimes called a nibble
A kilobyte (Kb) is 1024 bytes
A megabyte (Mb) is 1024 kilobytes
A gigabyte (Gb) is 1024 megabytes
A terabyte (Tb) is 1024 gigabytes
Computer Systems 1 (2004-2005)
Padding, Cladding and Loft Insulation (?!?)


Often when writing binary values we will use a notation method
called ‘padding’
This involves padding a value with zeros on the left up to the
nearest byte size





Extra zeros become insignificant
Makes reading and working with values easier
The first 1 encountered on the left is most significant bit (MSB)
The last 1 on the furthest right is least significant bit (LSB)
Example




Binary number 1011
Padded to a byte = 00001011
Binary 010
Padded to a byte = 00000010

This could have originally been just ‘10’ too!
Computer Systems 1 (2004-2005)
Converting Decimal & Binary

To convert decimal to binary:



If the decimal number is ODD, write a ‘1’ on the far right hand
side
OR if the decimal number is EVEN, write a ‘0’ on the far right
hand side
Divide the decimal number by TWO




If there is a remainder then ignore it
If the number you have is ODD, write a ‘1’ to the left of the
previous digit
OR if the number is EVEN, write a ‘0’ to the left of the previous
digit
Repeat until your decimal number is 0
Computer Systems 1 (2004-2005)
Converting Decimal & Binary

Decimal ’15’ = ODD =



15/2 = 7.5 = 7 = ODD = 11
7/2 = 3.5 = 3 = ODD = 111
3/2 = 1.5 = 1 = ODD = 1111


Binary ‘1111’ = Decimal ’15’ !!!!
We can ‘pad’ our binary number with 0’s


1
Eg: 00001111
Decimal ’32’ = EVEN =





0
32/2 = 16 = EVEN =
00
16/2 = 8 = EVEN =
000
8/2 = 4 = EVEN =
0000
4/2 = 2 = EVEN =
00000
2/2 = 1 = ODD =
100000


Binary ‘100000’ = Decimal ’32’ !!!!
We can ‘pad’ our binary number with 0’s

Eg: 00100000
Computer Systems 1 (2004-2005)
Converting Binary & Decimal

To convert binary to decimal:






Write out the binary number
Add the positional values to each digit
Where there is a ‘1’ below the positional value highlight that
positional value
Once all 1’s in the binary number have highlighted their
positional values
Add all highlighted values together
Positional values:


512
256
128
64
32
16
8
4
These values can of course increase

Doubled with each positional move left
Computer Systems 1 (2004-2005)
2
1
Converting Binary & Decimal

E.g.
Binary number: 00011011

Add positional values:


128
0

Highlight the positional values

Add the highlighted values together:


64
0
32
0
16
1
8
1
4
0
2
1
1
1
16+8+2+1 = 27
So Binary 00011011 = Decimal 27 !!
Computer Systems 1 (2004-2005)
Hexadecimal

Base 16
Another common format for data representation

Easy to map against binary
Used to easily represent large numbers




Common usage is memory locations
16 values then increment next digit on left position


0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
Each position represents a value of 16 to the positions power


160 (1), 161 (16), 162 (256), 163 (4096), 164 (65536), etc…
Hexadecimal numbers

E.g



5
10
FF
FB 19
Computer Systems 1 (2004-2005)
Hexadecimal Values
DECIMAL
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
HEXADECIMAL
Computer Systems 1 (2004-2005)
Converting Decimal & Hexadecimal

To convert decimal to hexadecimal:







Write out the decimal number
Write out the hexadecimal positional values
Find the largest hex positional value that your decimal number will
divide into
Divide your decimal number by that hex positional value
Take the answer( ignore the remainder) and add it to the left hand side
of your hex answer multiply this number by it’s positional value and
then subtract it from your decimal number
Take what’s left of your decimal number and repeat the process until
you have nothing left
We can also pad out Hex values with zeros if we want too!
Computer Systems 1 (2004-2005)
Converting Decimal &
Hexadecimal

E.g.

Decimal 280
Write Hex positional values









65536
4096
256
16
1
16
1
1
8
Divide 280 by 256 (=1)
Multiply 1 by 256 (=256)
Subtract 256 from 280 (280-256=24)
Divide 24 by 16(=1)
Multiply 1 by 16 (=16)
Subtract 16 from 24 (24-16=8)
Divide 8 by 1 (=8)
Write in the 8



1048576
1048576
0
65536
0
4096
0
256
1
Hex number = 118
Computer Systems 1 (2004-2005)
Converting Hexadecimal & Decimal

To convert hexadecimal to decimal:






Write out the hexadecimal number
Add the positional values to each digit
Where there is a value greater than ‘0’ below the positional
value highlight that positional value
Once all positional values have been highlighted, multiply the
number below, by it’s positional value
Add all results of highlighted values together
Positional values:


1048576
65536
4096
256
16
1
These values can of course increase to the power of 16
Computer Systems 1 (2004-2005)
Converting Hexadecimal &
Decimal

E.g.
Hex number 4C
Add positional values

1048576

0
0
0
0
4 C
Highlight positional values
Multiply positional values by the number below






65536
4096
256
16
1
4*16 = 64
C*1 = 12*1 = 12
Add these together

64+12=76
Computer Systems 1 (2004-2005)
Notation of Numerical Values

Very often we will have to write many different base value
numbers


Performing calculations or comparisons
For easy reading, the following conventions are used:

Decimal

Will either appear with no special notation or with a subscript ‘10’
after the number


Binary

Using subscript ‘2’ after the number


e.g.- 365 or 36510
e.g.- 100111012
Hexadecimal

Using a subscript ‘16’ after the number, of a prefix ‘0x’

e.g.- 4D16 or 0x4D
Computer Systems 1 (2004-2005)
CS1: Week 3

What you know now:



Decimal principles
Binary
 Method
 Notation
 Bits, bytes, etc.
 Conversion
Hexadecimal



Method
Notation
Conversion
Computer Systems 1 (2004-2005)
Download