Bits, Data Types, and Operation
Bits, Data Types, and Operation
Chapter 2
© 2020 McGraw-Hill Education. All rights reserved. Authorized only for instructor use in the classroom.
No reproduction or further distribution permitted without the prior written consent of McGraw-Hill Education.
How do we represent data in a computer?
We could base the state on the value of the voltage, but control and
detection circuits are more complex.
• Compare sticking a lightbulb (or your finger) in a socket versus using a
voltmeter.
© McGraw Hill 2
Computer is a Binary Digital system
© McGraw Hill 3
More than two values...
© McGraw Hill 4
What kinds of values do we want to represent?
© McGraw Hill 5
Data Type
© McGraw Hill 6
Unsigned Integers 1
© McGraw Hill 7
Unsigned Integers 2
22 21 20 value
0 0 0 0
0 0 1 1
0 1 0 2
0 1 1 3
1 0 0 4
1 0 1 5
1 1 0 6
1 1 1 7
© McGraw Hill 8
Unsigned Binary Arithmetic
© McGraw Hill 9
Signed Integers
How to represent positive (+) and negative (−)?
With n bits, we have 2n encodings.
• Use half of the patterns (the ones starting with zero) to represent
positive numbers.
• Use the other half (the ones starting with one) to represent negative
numbers.
• Signed-magnitude.
• 1's complement.
• 2's complement.
© McGraw Hill 10
Signed-Magnitude
© McGraw Hill 11
1's complement
To get a negative number, start with a positive number (with zero as the
leftmost bit) and flip all the bits -- from 0 to 1, from 1 to 0.
© McGraw Hill 12
Disadvantages of Signed-Magnitude and 1's Complement
© McGraw Hill 13
2's Complement
© McGraw Hill 14
2's Complement Integers
© McGraw Hill 15
Converting X to −X
A shortcut method:
Copy bits from right to left up to (and including) the first '1'.
Then flip remaining bits to the left.
© McGraw Hill 16
Converting Binary (2's C) to Decimal
© McGraw Hill 17
More Examples
X = 00100111two n 2n
= 25 + 22 + 21 + 20 = 32 + 4 + 2 + 1 0 1
In the
1 2
following
= 39 ten table,
2 ‘24n’
read
as 2 to
the
3 8n.
X = 11100110 two power
4 16
- X = 00011010 5 32
6 64
= 24 + 23 + 21 = 16 + 8 + 2 7 128
8 256
= 26 ten 9 512
© McGraw Hill 18
Converting Decimal to Binary (2's C) 1
© McGraw Hill 21
Converting Decimal Fraction to Binary (2's C)
1. Multiply fraction by 2.
2. Record one's digit of result, then subtract it from the decimal number.
3. Continue until you get 0.0000... or you have used the desired number
of bits (in which case you have approximated the desired value).
© McGraw Hill 22
Operation: Addition
© McGraw Hill 23
Operation: Subtraction
© McGraw Hill 24
Operation: Sign Extension
© McGraw Hill 25
Overflow
If the numbers are two big, then we cannot represent the sum using the
same number of bits.
For 2's complement, this can only happen if both numbers are positive
or both numbers are negative.
Another test (easier to perform in hardware): Carry-in to most significant bit position is
different than carry-out.
© McGraw Hill 26
Logical Operations: AND, OR, NOT
If we treat each bit as TRUE (1) or FALSE (0), then we define these
logical operations, also known as Boolean operations, on a bit.
A B A AND B A B A OR B A NOT A
0 0 0 0 0 0 0 1
0 1 0 0 1 1 1 0
1 0 0 1 0 1
1 1 1 1 1 1
© McGraw Hill 27
Examples of Logical Operations
AND
useful for clearing bits
• AND with zero = 0.
• AND with one = no change.
OR
useful for setting bits
• OR with zero = no change.
• OR with one = 1.
NOT
unary operation -- one argument
• flips every bit.
© McGraw Hill 28
Logical Operation: Exclusive-OR (XOR)
XOR
useful for selectively flipping bits
• XOR with zero = no change.
• XOR with one = flip.
© McGraw Hill 29
DeMorgan's Laws 1
There's an interesting relationship between AND and OR.
If we NOT two values (A and B), AND them, and then NOT the result,
we get the same result as an OR operation. (In the figure below, an
overbar denotes the NOT operation.)
A B A̅ B̅ A̅ AND B̅ A̅ AND B̅
0 0 1 1 1 0
0 1 1 0 0 1
1 0 0 1 0 1
1 1 0 0 0 1
Access the text alternative for slide images.
© McGraw Hill 30
DeMorgan's Laws 2
© McGraw Hill 31
Very Large or Very Small Numbers: Floating-Point
© McGraw Hill 32
Floating-point Example
© McGraw Hill 33
Converting from Decimal to Floating-Point Binary
Answer:
11000000110101000000000000000000
© McGraw Hill 34
Special Values
Infinity
© McGraw Hill 35
Representing Text: ASCII Code
© McGraw Hill 36
Hexadecimal Notation: Binary Shorthand
To avoid writing long (error-prone) binary values, group four bits together
to make a base-16 digit. Use A to F to represent values 10 to 15.
binary hexadecimal decimal binary hexadecimal decimal
(base 2) (base 16) (base 10) (base 2) (base 16) (base 10)
0000 0 0 1000 8 8
0001 1 1 1001 9 9
0010 2 2 1010 A 10
0011 3 3 1011 B 11
0100 4 4 1100 C 12
0101 5 5 1101 D 13
0110 6 6 1110 E 15
0111 7 7 1111 F 15
© McGraw Hill 37
Converting from binary to hex
Starting from the right, group every four bits together into a hex digit.
Sign-extend as needed.
© McGraw Hill 38
Because learning changes everything. ®
www.mheducation.com
© 2020 McGraw-Hill Education. All rights reserved. Authorized only for instructor use in the classroom.
No reproduction or further distribution permitted without the prior written consent of McGraw-Hill Education.