Lecture 1-Review-Binary - Logic-Basics

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Binary review

The digital world operated in binary


In order to understand how a computer / microprocessor / memory /
microcontroller / … works, one definitely needs to know binary and
hexadecimal numbers
Understanding Binary Numbers
• Binary numbers are made of binary digits (bits):
– 0 and 1
• binary number:
– (1011)2 = 1x23 + 0x22 + 1x21 + 1x20 = (11)10
• What about fractions?
– (110.10)2 = 1x22 + 1x21 + 0x20 + 1x2-1 + 0x2-2 = 6.5
• Groups of eight bits are called a byte
– (1100 1001) 2
• Groups of four bits are called a nibble.
– (1101) 2

From Digital Logic Design – M.Fakih


Converting binary to decimal
• We can use the same trick to convert binary, or base 2, numbers to
decimal. This time, the weights are powers of 2.
– Example: 1101.01 in binary
1 1 0 1 . 0 1 Binary digits, or bits
23 22 21 20 2-1 2-2 Weights (in base 10)

– The decimal value is:

(1 x 23) + (1 x 22) + (0 x 21) + (1 x 20) + (0 x 2-1) + (1 x 2-2) =


8 + 4 + 0 + 1 + 0 + 0.25 = 13.25

From Digital Logic Design – M.Fakih


Powers of 2 to MEMORIZE!!

Powers of 2: Useful abbreviations:

20 = 1 24 = 16 28 = 256 𝐾 = 210 = 1,024


21 = 2 25 = 32 29 = 512 M = 𝐾 2 = 220 = 1,048,576
22 = 4 26 = 64 210 = 1024 G = 𝐾 3 = 230 = 1,073,741,824
23 = 8 27 = 128

From Digital Logic Design – M.Fakih


Converting decimal to binary
• To convert a decimal integer into binary, keep dividing by 2 until the
quotient is 0. Collect the remainders in reverse order.
• To convert a fraction, keep multiplying the fractional part by 2 until it
becomes 0. Collect the integer parts in forward order.
• Example: 162.375:

162 / 2 = 81 rem 0 0.375 x 2 = 0.750


81 / 2 = 40 rem 1 0.750 x 2 = 1.500
40 / 2 = 20 rem 0 0.500 x 2 = 1.000
20 / 2 = 10 rem 0
10 / 2 =5 rem 0
5/2 =2 rem 1
2/2 =1 rem 0
1/2 =0 rem 1

• So, 162.37510 = 10100010.0112

From Digital Logic Design – M.Fakih


Base 16: Hexadecimal (Hex)
Hex symbols: 0 1 2 3 4 5 6 7 8 9 A B C D E F
2 methods to convert
• decimal to hex:
1. Convert decimal to bin then bin to hex
2. Convert directly dec to hex using successive
division
• Hex is easier to work with than binary
• more symbols can be used (easier to memorize)
• less confusing visually

• One hexadecimal digit ⇔ 4 binary digits (why?)


• B4 ≡ 1011 0100
• 2𝐷7𝐴 ≡ 0010 1101 0111 1010
• Easy conversion!! need to memorize the table →
Binary and hexadecimal conversions
• Converting from hexadecimal to binary is easy: just replace each hex digit with its equivalent
4-bit binary sequence.
261.316 = 2 6 1 . 3
= 0010 0110 0001 . 0011
• To convert from binary to hex, make groups of 4 bits, starting from the binary point. Add 0s to
the ends of the number if needed. Then, just convert each bit group to its corresponding hex
digit.

10110100.00102 = 1011 0100 . 0010


= B 4 . 2

Hex Binary Hex Binary Hex Binary Hex Binary


0 0000 4 0100 8 1000 C 1100
1 0001 5 0101 9 1001 D 1101
2 0010 6 0110 A 1010 E 1110
3 0011 7 0111 B 1011 F 1111

9
Hex to Decimal

(87C9)16 = (???)10

x 83 x 72 x C x 90
16 16 161 16
32,768 + 1792 + 192 + 9

(87C9)16 = (34,761)10

10

From Digital Logic Design – M.Fakih


Convert Decimal to Hex

Integer Part: Divide by the base,


keep track of the remainder, and read up.
16 34,761
16 2,172 rem 9
16 rem 12 = C Read up
13
5
16 rem 7
34,76110 = 87C916
8
0 rem 8
11

From Digital Logic Design – M.Fakih


Problems

100101012 = ? (decimal)

12
Problems

85710 = ?16

13
Problems

BED16 = ?2

14
Addition

15

From Digital Logic Design – M.Fakih


Binary Addition

• Binary addition is very simple.


• 0+1 = 1
• 1 + 1 = 0 (and carry 1)
• Watch for overflow!
• Example: 111101 + 10111
1 1 1 1 1 1 carries
1 1 1 1 0 1
+ 0 1 0 1 1 1
---------------------
1 0 1 0 1 0 0

Overflow

Mathematically, overflow is not a problem.


However, in a fixed-size storage, problems arise
Subtraction

18

From Digital Logic Design – M.Fakih


Two’s Complement
Two steps: •
1. Flip each bit (one’s complement)
2. Then add 1
Example: Find the 2’s complement of (0110 0101)2 and of its 2’s complement…

• N = 0110 0101
• Flipped = 1001 1010
+ 1
---------------
1001 1011 1001 1011
0110 0100
-N +1
---------------
0110 0101
N
Subtraction example

Subtract 35 from 73 by adding


73 01001001
-35
1 10111 01 35 00100011
38
00100110 2’s comp 11011100 flip
+1
-----------

-35 11011101

From Digital Logic Design – M.Fakih


Question: How many numbers can be represented
by n binary bits?

• Take n=2; • Take n=3


• 00 • 000
• 01 • 001
• 10 • 010
• 11 • 011
(4 numbers) • 100
• 101
• 110
• 111
(8 numbers)
Question: which of these numbers is positive, and
which is negative?
• Take n=2; • Take n=3
• 00 • 000
• 01 • 001
• 10 • 010
• 11 • 011
• (4 numbers) • 100
• 101
• 110
• 111 (8 numbers)
Answer: it depends on YOUR assumption
Unsigned numbers
• One can choose to consider all • Alternatively, one can choose to
numbers represented by n bits consider the n-bits as signed
to be unsigned (non-negative) numbers in two’s complement
• 00=0 form (MSB represents the sign)
• 01=1 • If MSB=1 ➔ negative
• 10=2 • 00=0
• 11=3 • 01=1
• 10=-1
• Range: 0 to 3 • 11=-2
• Range: -2 to +1
Conclusion (Very important!!)
1. still 4 = 22 = 2𝑛 numbers can be represented with 𝑛 bits
2. The range of unsigned numbers is 0 to 3 = 22 − 1 = 2𝑛 − 1
3. The range of signed –two’s complement – is:−2 to +1
−2𝑛−1 to +(2𝑛−1 − 1)
Verify how many numbers can be represented in each case

Unsigned Range: Signed Range:


0 to 2𝑛 − 1 −2𝑛−1 to +(2𝑛−1 − 1)
Verify how many numbers can be represented in each case

Unsigned Range:
0 to 2𝑛 − 1 Why?

2𝑛 − 1 − 0 + 1 = 2𝑛
Verify how many numbers can be represented in each case

Signed Range:
−2𝑛−1 to +(2𝑛−1 − 1)

+ 2𝑛−1 − 1 − −2𝑛−1 + 1 = 2𝑛−1 + 2𝑛−1 = 2 2𝑛−1 = 2𝑛

Why?
Try 𝑛 = 3
• Unsigned range: 0 to 7 = 23 − 1
✓000 001 010 011 100 101 110 111
✓0 1 2 3 4 5 6 7

• Signed range:
➢000 001 010 011 100 101 110 111
➢0 1 2 3 ? ? ? ?
Table 2.2
Positive and Negative Binary Numbers

Signed decimal Hex Binary Unsigned decimal


-128 80 10000000 128
-127 81 10000001 129
-126 82 10000010 130
… … … …
… … … …
… … … …
-3 FD 11111101 253
-2 FE 11111110 254
-1 FF 11111111 255
0 00 00000000 0
1 01 00000001 1
2 02 00000010 2
3 03 00000011 3
… … … …
… … …
… … …
125 7D 01111101 125
126 7E 01111110 126
127 7F 01111111 127

23

From Digital Logic Design – M.Fakih


Signed Numbers

4-bit: 8H = -8 to 7H = +7
1000 to 0111

8-bit: 80H = -128 to 7FH +127


=
16-bit: 8000H = -32,768 to
7FFFH = +32,767

32-bit: 80000000H = -2,147,483,648 to


7FFFFFFFH = +2,147,483,647

24

From Digital Logic Design – M.Fakih


Questions

What is the two’s complement of


00101100?

What hex number represents the


decimal number -40?

25

From Digital Logic Design – M.Fakih


General information

ASCII
Unicode
Utf-8, 16
28

From Digital Logic Design – M.Fakih


ASCII

• The most commonly used code for representing letters, numerals


and punctuation characters (alphanumeric data)
• Each character is represented with a 7-bit string; for example:
‘3’ = 00110011 (hex 33)
‘ ’ = 00100000 (hex 20)
• An 8-bit extension of ASCII has also been defined

29

From Digital Logic Design – M.Fakih


ASCII Code
• American Standard Code for Information Interchange
• ASCII is a 7-bit code, frequently used with an 8th bit for error
detection (more about that in a bit).

Character ASCII (bin) ASCII (hex) Decimal


A 1000001 41 65
B 1000010 42 66
C 1000011 43 67

Z
a

1

30

From Digital Logic Design – M.Fakih


ASCII
Table
Extended
ASCII
How are other languages represented?
Arabic, Chinese, .. alphabets
• Answer: search for Unicode, utf-8, utf-16

• https://www.w3schools.com/charsets/ref_html_utf8.asp
• https://stackoverflow.com/questions/643694/what-is-the-difference-
between-utf-8-and-unicode

You might also like