Binary Numbers
 Strings of binary digits (“bits”)
 One bit can store a number from 0 to 1
 n bits can store numbers fro...
Binary – Powers of 2
 Positional representation
 Each digit represents a power of 2
So 101 binary is
1 • 22 + 0 • 21 + 1...
Converting Binary to Decimal
 Easy, just multiply digit by power of 2
 Just like a decimal number is represented
 Examp...
Binary

Decimal Example

7

6

5

4

3

2

1

0

27

26

25

24

23

22

21

20

128

64

32

16

8

4

2

1

What is 1001...
Decimal to Binary
 A little more work than binary to decimal
 Some examples
 3 = 2 + 1 = 11 (that‟s 1•21 + 1•20)
 5 = ...
Algorithm – Decimal to Binary
 Find largest power-of-two smaller than decimal

number
 Make the appropriate binary digit...
Decimal

Binary Example

 Convert 28 decimal to binary

32 is too large, so use 16
Binary
10000
Decimal
Next is 8
Binary
...
Hexadecimal
 Strings of 0s and 1s too hard to write
 Use base-16 or hexadecimal – 4 bits

Dec

Bin

Hex

Dec

Bin

Hex

...
Hexadecimal
 Letters to represent 10-15

Dec

Bin

Hex

Dec

Bin

Hex

0

0000

0

8

1000

8

1

0001

1

9

1001

9

2
...
Hex to Binary
Bin

Hex

0000

0

0001

1

0010

2

 Convention – write 0x before number

0011

3

 Hex to Binary – just ...
Binary to Hex

Bin
0000

0011
0100
0101

3

7

b

101001101111011 = 0x537b

5

0110

1011

4
6

0111

7

1000

8
9
a

1011...
Hex to Decimal

Dec

Hex

0

0

 Just multiply each hex digit by decimal value,

1

1

2

2

3

3

4

4

5

5

6

6

7

7...
Decimal to Hex
1.

2.
3.
4.

Analogous to decimal
binary.
Find largest power-of-16 smaller than decimal
number
Divide by p...
Decimal to Hex

Dec

Hex

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

10

a

11

b

12

c

13

d

160

14
...
Octal
 Octal is base 8
 Similar to hexadecimal
 Conversions

 Less convenient for use with 8-bit bytes

15
Arithmetic -- addition
 Binary similar to decimal arithmetic

1

No carries

0

1

1

0

0

0

1

1

0

0

1

0

1

1

0
...
Arithmetic -- subtraction

0

1
-

0

1

1

1

0

0

1

0

0

0

1

0

0

-

1

1

0

1

1

1

0

1

0

0

1

1

0

0

0

...
Arithmetic -- multiplication

1

0

1

1

1

0

1

1

0

1

1

0

0

0

0

1

0

1

1

1

1

0

1

X

1

Successive additi...
Hexadecimal Arithmetic
 Similar
 If you‟re doing by hand, easiest to convert each set

of digits to decimal and back

19
Upcoming SlideShare
Loading in …5
×

01 intro

243 views

Published on

  • Be the first to comment

  • Be the first to like this

01 intro

  1. 1. Binary Numbers  Strings of binary digits (“bits”)  One bit can store a number from 0 to 1  n bits can store numbers from 0 to 2n 1
  2. 2. Binary – Powers of 2  Positional representation  Each digit represents a power of 2 So 101 binary is 1 • 22 + 0 • 21 + 1 • 20 or 1•4 + 0•2 + 1•1=5 2
  3. 3. Converting Binary to Decimal  Easy, just multiply digit by power of 2  Just like a decimal number is represented  Example follows 3
  4. 4. Binary Decimal Example 7 6 5 4 3 2 1 0 27 26 25 24 23 22 21 20 128 64 32 16 8 4 2 1 What is 10011100 in decimal? 1 0 0 1 1 1 0 0 128 + 0 + 0 + 16 + 8 + 4 + 0 + 0 = 156 4
  5. 5. Decimal to Binary  A little more work than binary to decimal  Some examples  3 = 2 + 1 = 11 (that‟s 1•21 + 1•20)  5 = 4 + 1 = 101 (that‟s 1•22 + 0•21 + 1•20) 5
  6. 6. Algorithm – Decimal to Binary  Find largest power-of-two smaller than decimal number  Make the appropriate binary digit a „1‟  Subtract the power of 2 from decimal  Do the same thing again 6
  7. 7. Decimal Binary Example  Convert 28 decimal to binary 32 is too large, so use 16 Binary 10000 Decimal Next is 8 Binary 11000 Decimal Next is 4 Binary 11100 Decimal 28 – 16 = 12 12 – 8 = 4 4–4=0 7 6 5 4 3 2 1 0 27 26 25 24 23 22 21 20 128 64 32 16 8 4 2 1 7
  8. 8. Hexadecimal  Strings of 0s and 1s too hard to write  Use base-16 or hexadecimal – 4 bits Dec Bin Hex Dec Bin Hex 0 0000 0 8 1000 8 1 0001 1 9 1001 9 2 0010 2 10 1010 ? 3 0011 3 11 1011 ? 4 0100 4 12 1100 ? 5 0101 5 13 1101 ? 6 0110 6 14 1110 ? 7 0111 7 15 1111 ? 8
  9. 9. Hexadecimal  Letters to represent 10-15 Dec Bin Hex Dec Bin Hex 0 0000 0 8 1000 8 1 0001 1 9 1001 9 2 0010 2 10 1010 a 3 0011 3 11 1011 b 4 0100 4 12 1100 c 5 0101 5 13 1101 d 6 0110 6 14 1110 e •Power of 2 7 0111 7 15 1111 f •Size of byte Why use base 16? 9
  10. 10. Hex to Binary Bin Hex 0000 0 0001 1 0010 2  Convention – write 0x before number 0011 3  Hex to Binary – just convert digits 0100 4 0101 5 0110 6 0111 7 1000 8 1001 9 1010 a 1011 b 1100 c 1101 d 1110 e 1111 f 0x2ac 0010 1010 1100 0x2ac = 001010101100 No magic – remember hex digit = 4 bits 10
  11. 11. Binary to Hex Bin 0000 0011 0100 0101 3 7 b 101001101111011 = 0x537b 5 0110 1011 4 6 0111 7 1000 8 9 a 1011 b 1100 c 1101 5 0111 3 1010 0011 2 1001 0101 1 0010 101001101111011 0 0001  Just convert groups of 4 bits Hex d 1110 e 1111 f 11
  12. 12. Hex to Decimal Dec Hex 0 0  Just multiply each hex digit by decimal value, 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 a 11 b 12 c 13 d 14 e 15 f and add the results. 0x2ac 2 • 256  + 10 • 16 + 12 • 1 = 684    163 162 161 160 4096 256 16 1 12
  13. 13. Decimal to Hex 1. 2. 3. 4. Analogous to decimal binary. Find largest power-of-16 smaller than decimal number Divide by power-of-16. The integer result is hex digit. The remainder is new decimal number. Do the same thing again 13
  14. 14. Decimal to Hex Dec Hex 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 a 11 b 12 c 13 d 160 14 e 1 15 f 684 0x2__ 684/256 = 2 684%256 = 172 0x2a_ 172%16 = 12 = c 0x2ac 172/16 = 10 = a  163 4096  162 256  161 16  14
  15. 15. Octal  Octal is base 8  Similar to hexadecimal  Conversions  Less convenient for use with 8-bit bytes 15
  16. 16. Arithmetic -- addition  Binary similar to decimal arithmetic 1 No carries 0 1 1 0 0 0 1 1 0 0 1 0 1 1 0 + 1 0 0 0 1 + 1 0 1 1 1 1 1 1 0 1 1 1 1 0 Carries 1 0 1+1 is 2 (or 102), which results in a carry 16
  17. 17. Arithmetic -- subtraction 0 1 - 0 1 1 1 0 0 1 0 0 0 1 0 0 - 1 1 0 1 1 1 0 1 0 0 1 1 0 0 0 1 No borrows 1 0 1 1 Borrows 0 - 1 results in a borrow 17
  18. 18. Arithmetic -- multiplication 1 0 1 1 1 0 1 1 0 1 1 0 0 0 0 1 0 1 1 1 1 0 1 X 1 Successive additions of multiplicand or zero, multiplied by 2 (102). Note that multiplication by 102 just shifts bits left. 1 18
  19. 19. Hexadecimal Arithmetic  Similar  If you‟re doing by hand, easiest to convert each set of digits to decimal and back 19

×