Professional Documents
Culture Documents
Decimal Numbers
In decimal (base 10), every digit can range from 0 to 9
150 in decimal could be thought of as
Digit
1
5
Place 102 = 100 101 = 10
Decimal 1 * 100 = 5 * 10 =
Value
100
50
0
100 = 1
0*1=
0
Binary
Binary numbers are in base 2
This means that every digit can only be a 1 or a 0
So for example, 101 in binary would be represented as:
Digit
Place
Decimal
Value
1
22 = 4
1*4=
4
0
21 = 2
0*2=
0
1
20 = 1
1* 1 =
1
Powers of 2
In order to easily convert from decimal to binary, you need to
know the powers of 2
Power
Value
20
21
22
23
24
16
25
32
26
64
27
128
28
256
Digit
Place
1
25
24
3. 2 != 0, so continue
23
22
21
20
Power
Value
20
21
22
23
24
16
25
32
26
64
27
128
28
256
Digit
Place
1
25
3. 0 == 0, so stop
24
23
22
1
21
20
Power
Value
20
21
22
23
24
16
25
32
26
64
27
128
28
256
Digit
Place
1
25
0
24
0
23
0
22
1
21
0
20
Power
Value
20
21
22
23
24
16
25
32
26
64
27
128
28
256
Bytes
A byte is 8 bits
0 0 1 0 0 0 1 0
char type
A char is a whole number type thats represented by a single byte
Overflow
Since an unsigned char can only range from 0 to 255, what
happens with the following code?
Overflow, Contd
With the code on the previous slide:
Overflow, Contd
Start with 255
1 1 1 1 1 1 1 1
Overflow, Contd
Add 1 (for the ++)
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 1
Overflow, Contd
+
=
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
Hexadecimal
Hexadecimal (or hex) is base 16, so the digits range from 0 to F
Hex
Digit
09
Decimal
Value
(Same)
A
B
C
10
11
12
D
E
F
13
14
15
0 0 1 0 1 1 0 1
0 0 1 0 1 1 0 1
2
8+4+1=13
0 0 1 0 1 1 0 1
2
8+4+1=13
2
D
So 45 is 00101101 in binary, and 2D in hex
Lab Practical #8