You are on page 1of 14

Chapter 2: Bits, Data Types And Operations

2.1 Bits And Data Types And Operations


2.1.1 The Bit As The Unit Of Information
2.1.2 Data Types

2.2 Integer Data Types


2.2.1 Unsigned Integers
2.2.2 Signed Integers
2.3 2's Complement Integers
2.4 Conversion Between Binary And Decimal
2.4.1 Binary To Decimal Conversion
2.4.2 Decimal To Binary Conversion
2.4.3 Extending Conversion To Numbers With Fractional Parts
2.5 Operations On Bits - Part 1: Arithmetic
2.5.1 Addition And Subtraction
2.5.2 Sign-Extension
2.5.3 Overflow
2.6 Operations On Bits - Part 2: Logical Operations
2.6.1 A Logical Variable
2.6.2 The AND Function
2.6.3 The OR Function
2.6.4 The NOT Function
2.6.5 The Exclusive-Or Function
2.6.6 DeMorgan's Laws
2.6.7 The Bit Vector
2.7
2.7.1 Floating Point Data Type (Greater Range, Less Precision)
2.7.2 ASCII Codes
2.7.3 Hexadecimal Notation
Exercises

Notes

Chapter 2: Bits, Data Types And Operations


2.1 Bits And Data Types And Operations
2.1.1 The Bit As The Unit Of Information

In chapter 1 we saw that the computer is organized as a system with several levels
of transformation, and ultimately works by electrons moving, which in turn are
controlled by voltage levels in circuits. Electrons react to actual voltage values,
and not the presence or absence of these voltages.
But it is easier to think of the presence/absence of voltages.

We represent the presence of voltage symbolically with 1, and absence with 0.


We refer to each 0 or 1 as a 'bit'. (shortened form of binary digit)

2.1.2 Data Types

There are many ways to represent the same value.


The number 5 can be represented as "5", a hand gesture with palm facing forward
with all five fingers extended, binary 00000101, the roman V etc
It is not enough to represent values. We want to operate on them.

A representation is called a data type if there exist operations in the computer


that can operate on information encoded in that representation.

Each ISA has its own set of datatypes and operations (instructions that operate on
that datatype).

In this book we use two datatypes - integers in 2's complement (tbd) and characters
as ASCII codes.
In real world computers there are other datatypes, e.g floating point.

2.2 Integer Data Types


2.2.1 Unsigned Integers
The first representation of information, or datatype, that we'll look at is the
unsigned integer.
An unsigned integer has no sign (+ or -) associated with it, just a magnitude.
Unsigned integers are used as counters, memory location identifiers etc.

represented as strings of binary digits.

Decimal 329 = 3 * 10^2 + 2 * 10^1 + 9 * 10^0

similarly 00101 = 0 * 2^4 + 0 * 2^3 + 1 * 2^2 + 0 * 2^1 + 1 * 2^0 = 4 + 1


(decimal) = 5 decimal.

We can, with k bits, represent 2^k integers from 0 to (2^k - 1)

Basic INSIGHT == for four bits, we have

00000
00001
...

11111

so 32 patterns. Signed, Unsigned, One's complement, Two's complement etc are


schemes of matching these 32 patterns to 32 values.

The unsigned scheme = Each pattern represents the integer value obtained by direct
binary-to-integer conversion

so

00000 = 0
00001 = 1
00010 = 2
00011 = 3
00100 = 4
00101 = 5
00110 = 6
00111 = 7
01000 = 8
01001 = 9
01010 = 10
01011 = 11
01100 = 12
01101 = 13
01110 = 14
01111 = 15
10000 = 16
10001 = 17
10010 = 18
10011 = 19
10100 = 20
10101 = 21
10110 = 22
10111 = 23
11000 = 24
11001 = 25
11010 = 26
11011 = 27
11100 = 28
11101 = 29
11110 = 30
11111 = 31

So the range is from 0 to 31

2.2.2 Signed Integers

In the "signed magnitude" representation, the concept is that the left most bit
indicates the sign 1 for + , 0 for -, and the remaining bits, converted to
decimal, indicate the decimal number.

00000 = + 0
00001 = + 1
00010 = + 2
00011 = + 3
00100 = + 4
00101 = + 5
00110 = + 6
00111 = + 7
01000 = + 8
01001 = + 9
01010 = + 10
01011 = + 11
01100 = + 12
01101 = + 13
01110 = + 14
01111 = + 15
10000 = - 0
10001 = - 1
10010 = - 2
10011 = - 3
10100 = - 4
10101 = - 5
10110 = - 6
10111 = - 7
11000 = - 8
11001 = - 9
11010 = - 10
11011 = - 11
11100 = - 12
11101 = - 13
11110 = - 14
11111 = - 15

So the range is from -15 to +15 with *two* patterns for 0, 00000 and 10000.

In the "one's complement" representation the idea is that inverting the pattern of
a positive number should give the representation of the same number negated.
so
00000 = 0 so -0 must = 11111
00001 = 1 so -1 must == 11110

so we get
00000 = + 0
00001 = + 1
00010 = + 2
00011 = + 3
00100 = + 4
00101 = + 5
00110 = + 6
00111 = + 7
01000 = + 8
01001 = + 9
01010 = + 10
01011 = + 11
01100 = + 12
01101 = + 13
01110 = + 14
01111 = + 15
10000 = - 15
10001 = - 14
10010 = - 13
10011 = - 12
10100 = - 11
10101 = - 10
10110 = - 9
10111 = - 8
11000 = - 7
11001 = - 6
11010 = - 5
11011 = - 4
11100 = - 3
11101 = - 2
11110 = - 1
11111 = - 0

Note: Again the range is from -15 to +15 with two numbers for 0, 00000 and 11111.

2.3 2's Complement Integers

The concept here is based on how ALU's add binary numbers.


The ALU in computers is where addition happens.
Addition of binary strings happen right to left, with carry, as with decimal
addition. (KEY) any carry that goes over the leftmost 'limit' is thrown away.

00110+ in decimal 6 +
00101 5
----- ---
01011 11

now we say that 2's complement should give the effect that when a positive integer
is added to its negative equivalent we should get 0
if 1 = 00001

then -1 = ----- s.t

00001 + in decimal, 1 +
----- = -1 =

00000 0

with this condition we get - 1 being 11111 (note that leftmost carry is thrown
away)

00001+
11111
-----
00000

Similarly, 2 = 00010, then -2 is 11110 so that

00010+
11110
------
00000

A shortcut to generate 2's complement is, invert the number, then add 1

decimal 2 is 00010
flip to get 11101
then add 1 to get
11110 = -2 in 2s complement.

With this scheme we get


00000 = + 0
00001 = + 1
00010 = + 2
00011 = + 3
00100 = + 4
00101 = + 5
00110 = + 6
00111 = + 7
01000 = + 8
01001 = + 9
01010 = + 10
01011 = + 11
01100 = + 12
01101 = + 13
01110 = + 14
01111 = + 15
10000 = - 16
10001 = - 15
10010 = - 14
10011 = - 13
10100 = - 12
10101 = - 11
10110 = - 10
10111 = - 9
11000 = - 8
11001 = - 7
11010 = - 6
11011 = - 5
11100 = - 4
11101 = - 3
11110 = - 2
11111 = - 1

So now the range is -16 to -15. Note 1: Zero has only one representation, 00000.
note 2: the negative range is one more (0 to -16) than the positive range (0 to 15)

The LC3 works with 16 bit values, which (with 2's complement representation) can
represent integers from -32768 to 32767

2.4 Conversion Between (Two's complement) Binary And Decimal


2.4.1 (Two's Complement) Binary To Decimal Conversion

Step 1: If the left most digit is 0, number is positive, goto step


If the leftmost digit is 1, number is negative, take the *** 2s ****
complement, goto step

Step 2: Then (-1)^leftmost digit * sigma_i(b_i * 2^i).

Example: Convert 11000111 to a decimal number

the leftmost digit is 1, so negative number. take 2s complement


11000111

generate two's complement. complement + 1 = 00111000 + 1 = 00111001

check
11000111
00111001 <-- this is what we need to convert into decimal magnitude
--------
00000000

so we get

then (-1) *[2^5 + 2^4 + 2^3+2^0] = 32 + 16 + 8 + 1] = (-1) * 57 = -57

2.4.2 Decimal To (Two's Complement) Binary Conversion

The basic algorithm =


Step 1 if decimal number is odd, rightmost binary digit is 1, subtract 1 from
decimal, mark rightmost bit as 1
if decimal number is even, rightmost binary digit is 0, mark rightmost
digit as 0

in either case you end up with an even number on the left.

Step 2 divide both sides by 2. (on the right side, ignore the right most bit,
consider only the n-1 bits from the left.

Got to step 1, till all bits are filled. pad as necessary with zeros to fill
the word size

Step 3 if decimal number is -ve, the leftmost digit should be 0, else add a
leftmost 0 *and take 2's complement*.
if decimal number is +ve, the left most digit should be 0. Done

E.g: 8 bits, convert decimal -57


we start with (ignore

-57 = _ _ _ _ _ _ _ _

we just focus on the magnitude first so we get

57 = _ _ _ _ _ _ 1 (rightmost bit is 1 since number is odd)


subtract 1 from each side to get
[56 = _ _ _ _ _ _ 0] + 1
==
[56 = b_7 * 2^7 + b_6 * 2^6 + b_5* 2^5 + b_4 * 2^4 + b_3 * 2^3 + b_2 * 2^2 + b_1 *
2^1 + 0 * 2^0] + 1

==
div by 2 on both sides to get
[28 = b_7 * 2^6 + b_6 * 2^5 + b_5* 2^4 + b_4 * 2^3 + b_3 * 2^2 + b_2 * 2^1 + b_1 *
2^0] 1

== since lhs number is even, right most digit is zero

[28 = b_7 * 2^6 + b_6 * 2^5 + b_5* 2^4 + b_4 * 2^3 + b_3 * 2^2 + b_2 * 2^1 + 0 *
2^0] 1

==

[28 = b_7 * 2^6 + b_6 * 2^5 + b_5* 2^4 + b_4 * 2^3 + b_3 * 2^2 + b_2 * 2^1 + 0] 1

divide both sides by two to get

[14 = b_7 * 2^5 + b_6 * 2^4 + b_5* 2^3 + b_4 * 2^2 + b_3 * 2^1 + b_2 * 2^0] 0 1

== since lhs is even, rightmost digit is 0

[14 = b_7 * 2^5 + b_6 * 2^4 + b_5* 2^3 + b_4 * 2^2 + b_3 * 2^1 + 0 * 2^0] 0 1

==

[14 = b_7 * 2^5 + b_6 * 2^4 + b_5 * 2^3 + b_4 * 2^2 + b_3 * 2^1 + 0 ] 0 1

== lhs is even divide by 2

[7 = b_7 * 2^4 + b_6 * 2^3 + b_5* 2^2 + b_4 * 2^1 + b_3 * 2^0] 0 0 1

== lhs is odd, right most digit is 1

[7 = b_7 * 2^4 + b_6 * 2^3 + b_5* 2^2 + b_4 * 2^1 + 1 * 2^0] 0 0 1

subtract one from each side,


[6 = b_7 * 2^4 + b_6 * 2^3 + b_5 * 2^2 + b_4 * 2^1 + 0 * 2^0] + 1 0 0 1
divide by 2 to get

[3 = b_7 * 2^3 + b_6 * 2^2 + b_5* 2^1 + b_4 * 2^0] 1 0 0 1

== lhs is odd, so rightmost digit is 1


[3 = b_7 * 2^3 + b_6 * 2^2 + b_5* 2^1 + 1 * 2^0] 1 0 0 1

== lhs is odd, subtract 1,

[2 = b_7 * 2^3 + b_6 * 2^2 + b_5 * 2^1 + 0] + 1 1 0 0 1

divide by 2

[1 = b_7 * 2^2 + b_6 * 2^1 + b_5 * 2^0] 1 1 0 0 1

[0 0 1]1 1 0 1 1
==

0 0 1 1 1 0 1 1

This is the binary representation for + 57.

We take the 2's complement of this to get the representation for -57, getting,

-57 = 11000111

2.4.3 Extending Conversion To Numbers With Fractional Parts

What if the number we want to convert has a fractional part?

Binary to Decimal

0.b_1 b_2 b_3 b_4

0.b_1 * 2^(-1) + b_2 * 2^(-2) + b_3 * 2^(-3) + b_4 * 2^(-4)

Example

0.1011 = 0. [2^-1 + 2^-3 + 2^-4] = 0.[1/2 + 1/8 + 1/16] = 0.[11/16] = 0.6875

Decimal To Binary

in converting decimal to binary, we divided by 2 and assigned 1 or 0 to the co-


efficient of 2^0 depending on whether the number of the left of the equality sign
was odd or even

Here we *multiply by two* and if the number on the left side of the number is < 1
we assign 0 to the co efficient of 2^0 and if it is 1 0, we assign one, and reduce
the number on the left by 1, and repeat (note: we *first* multiply by 2, as it
stands there is no coefficient of 2^0, since the binary digits in the beginning are
multiplied with 2^-1, 2^-2 etc

0.421 = 0.b_1 * 2^(-1) + b_2 * 2^(-2) + b_3 * 2^(-3) + b_4 * 2^(-4)

multiply by 2 give

0.842 = 0. [b_1 * 2^0 + b_2 * 2^(-1) + b_3 * 2^(-2) + b_4 * 2^(-3) ...]

since number of left < 0 b_1 is 0 so giving


0.842 = 0. 0 [ b_2 * 2^(-1) + b_3 * 2^(-2) + b_4 * 2^(-3) ...]

multiply by 2 gives

1.684 = 1. 0 [ b_2 * 2^0 + b_3 * 2^(-1) + b_4 * 2^(-2) ...]

since number on left > 1 b_2 is 1

1.684 = 1. 0 1 [ b_3 * 2^(-1) + b_4 * 2^(-2) ...]

We reduce by 1 on each side to get

0.684 = 0. 0 1 [ b_3 * 2^(-1) + b_4 * 2^(-2) ...]

multiply by 2 to get

1.368 = 1. 0 1 [ b_3 * 2^0 b_4 * 2^(-1) ...]

number on left > 1, so b_3 is one, subtract one on each side to get

0.368 = 0 . 0 1 1 [b_4 * 2 ^ (-1) ...]

multiply by 2

0.736 = 0. 0 1 1 [b_4 * 2^0]

number on left < 0 so

0.736 = 0. 0 1 1 0 [...]

Note that converting 0.0110 = 1/4 + 1/8 = 0.25 + 0.125 = 0.375 which only
*approximates* to 0.421

With more digits in the binary number, the approximation will come closer.

2.5 Operations On Bits - Part 1: Arithmetic

2.5.1 Addition And Subtraction


addition is just as with decimal, but instead of generating carry after 9 in
decimal (9 is the largest digit in decimal) we generate a carry after 1 (1 is the
largest digit in binary).

So using 5 bit binary, what is 11 + 3?

the decimal 11 is 01011


the decimal 3 is 00011
sum decimal 14 is 01110

Subtraction is just addition with the negative of number to be subtracted

the decimal 14 is 01110 . the decimal 9 is 01001. its negative is 10111

adding -9 to 14 we get

14 01110 -
9 10111 is
5 00101

what when a number is added to itself

5 00101 +
5 00101
01010 = 10 decimal

note: the number 5 shifts left when doubled.

why does this happen?

5 is 00101 = [0 * 2^4 + 0 * 2^3 + 1 * 2^2 + 0 * 2^1 + 1 * 2^0 ]

multiplying by 2 we get 10 = 2 * [0 * 2^4 + 0 * 2^3 + 1 * 2^2 + 0 * 2^1 + 1 * 2^0 ]

which is 2 * [0 * 2^4 + 0 * 2^3 + 1 * 2^2 + 0 * 2^1 + 1 * 2^0 ]

== [0 *2^5 + 0 *2^4 + 1 *2^3 + 1 * 2^3 + 0 * 2^2 + 1 * 2^1 + 0 * 2^0]

iow the co-ordinate of 2^n in the old number is now the co-ordinate of 2^(n+1) in
the doubled number, with an extra zero padding the right

this is 'left shift'.

2.5.2 Sign-Extension
the basic idea is that
for positive 2s complement numbers, adding 0s to the left does not change the
number.
for negative 2s complement numbers, adding 1s to the left does not change the
number.

This fact can be used to pad the leftsides of 2's complement numbers, thus bringing
two operands of an operation to the same bit size.

this 'left-extension' is called 'sign-extentsion' often reduced to 'sext'

2.5.3 Overflow
happens when the bit width is not sufficient to hold the result of an operation.

2.6 Operations On Bits - Part 2: Logical Operations

A new class of operations (other than addition and subtraction). logical


operations.

2.6.1 A Logical Variable

has one of two values 0 or 1

2.6.2 The AND Function


AND is a binary logical function. the result of the AND logical function is 1 only
if both its arguments (or sources) are 1.

Truth tables are convenient ways to represent logical operations

AND Truth Table


AND

x y x AND y
0 0 0
0 1 0
1 0 0
1 1 1

we can apply the logical operation AND to two n bit operators. the AND operation is
applied independently to each pair of bits in the two operands

so

0011101001101001 AND
0101100100100001 is

0001100000100001

Bit masks (use of AND logical operation)

Suppose there is a bit pattern of which a subset of bits are important for some
reason.

We can AND the source bit pattern with a pattern which has all zeros except for
the bits of importance, where it is 1
and the AND operation will extract the required bits into a new pattern with all
other bits being zero

2.6.3 The OR Function

OR Truth Table

OR

x y x OR Y
0 0 0
0 1 1
1 0 1
1 1 1

2.6.4 The NOT Function

NOT Truth Table

x NOT X
0 1
1 0
2.6.5 The Exclusive-Or Function

XOR Truth Table


x y x XOR y
0 0 0
0 1 1
1 0 1
1 1 0
2.6.6 DeMorgan's Laws
2.6.7 The Bit Vector

An n bit pattern where each bit a logical value and operations are performed
bitwise is called a bit vector.

2.7
2.7.1 Floating Point Data Type (Greater Range, Less Precision)
Most arithmetic uses integer values. LC3 uses 16 bit 2 complement integers. The
data type uses 1 bit to identify positive or negative and the remaining 15 bits
hold the value. With 16 bits the value ranges from -32768 to +32767. We say the
precision is 15 bits and the range is 2^16

But we need to represent much larger numbers.


E.g: 6.022 * 10^23

The range needed to express 10^23 is far larger than the largest positive value
2^15-1 that is possible with 16 bits (and 2s complement).

However the *precision* associated with 2's complement integer is *more* than what
we require to represent 10^23. We only need to represent 6022 and 23 as exponent
(assuming 10 as the base).

So the problem is we don't have enough range, but we do have more than enough
precision.

The floating point datatype solves this problem.


Most ISAs have more than one floating point datatype. one of them, 'float' has the
following structure (with 32 bits)

Its structure is
S: 1 bit for sign (+/-)
E: 8 bits for exponent
F: 23 bits for the fraction

2.7.1.1 Normalized Form

The floating point data type *mostly* represents large numbers in the 'normalized
form'

N = (-1)^S * 1.fraction*2^(exponent-127) with 1 <= exponent <= 254

where S, fraction and exponent are the components of the structure above.

"mostly" represents floating point numbers in normalized form, because there is a


condition attached to trigger this.
the exponent must have values 1 <= x <= 254. There are two more values possible in
the 8 bit exponent, 0 and 255. When the exponent has these (two) values, it does
not represent a floating point value in the normalized form (see sections 2.7.1.2
and 2.7.1.3)

The exponent bits (8 bits) use what is called an excess code. the idea = treat the
8 bits as an unsigned integer, then subtracting an excess value == bias value) .
For 32 bit integers this is 127.

Thus an exponent field containing 10000110 has a raw value of decimal unsigned
integer 134, from which we reduce 127 , getting 7.
Exponent field containing 00000111 has a raw value of 7, from which we reduce 127
to get -120.
Range of normalized form's *exponent*
largest number it can hold is (given the constraint on it) 254 = 11111110 which
gives us (after subtracting 127) 2^127
smallest number it holds (subject to range constraint) is 00000001, =
decimal 1, which after subtracting 127 gives us 2^-126

example: What does the floating point data type 00111101100000000000000000000000


represent?

breaking into sign, exponent and fraction


0 01111011 00000000000000000000000

0 in the sign bit == positive number


01111011 in the exponent = unsigned number 123, subtracting 127 we get - 4
the 23 bit fraction holds 0

so this is 1.0*2^(-4) = 1/16

example: how to represent -6 5/8 in floating point?

as 'plain binary'. this is -110.101

== -1.10101 * 2^2

now normalizing

sign bit is 1 indicating a negative number


exponent should be 127+2 = 129. Using 8 bits this is 10000001
the fraction is 101. Using 23 bits, this is (note after decimal, so zero padding on
right side) 10101000000000000000000

so -1.1000000110101000000000000000000

example: 0 10000011 00101000000000000000000 is 1.00101 ⋅ 2^4 = 18.5

exponent field is 131. -127 = 4


fraction = 00101 = so number is 1.00101 * 2^4 = 10010

Example: 1 10000010 00101000000000000000000 is −1 ⋅ 1.00101 ⋅ 23 = −9.25


the sign bit is 1 so negative number.
exponent is 130. -127 gives us 3.
fraction + leading 1 is 1.00101000000000000000000 * 2^3

fraction + exponent = - 1.00101000000000000000000 * 2^3 = -1001.01 = -9.25

Example: 0 11111110 11111111111111111111111 is 2^128


sign bit is 0 so positive number
exponent is 254 so -127 gives 127
fraction is 11111111111111111111111

so number is 1.11111111111111111111111 * 2^127

the 1.111... is *approximately* 2.

so final number is 2^128


Normalized form does not handle numbers where exponent is 00000000 or 11111111.

If the exponent is 11111111 we use the floating point datatype to represent many
things, including infinity.
Infinity is represented by exponent containing all 1s and fraction field containing
all zeros.
sign bit represents positive or negative infinity.

2.7.1.3 Sub normal numbers.

The smallest number that can be represented in normalized form is

N = 1.00000000000000000000000 × 2^−126

what if we want numbers less than this but > 0

subnormal numbers have the form

N = (−1)S × 0. fraction × 2−126

the smallest such number is

= 0.00000000000000000000001 × 2−126.

the largest is
N = 0.11111111111111111111111 × 2^−126

2.7.2 ASCII Codes


Each key on the keyboard is represented by its unique ASCII code. digit 3 is
00110011

2.7.3 Hexadecimal Notation

so far, information can be represented as 2s complement integer, as bit vectors,


floating point format or ASCI codes.

the basic idea is each 4 bit binary number is represented by it hex equivalent

so

0011 1101 0110 1110 becomes

3 D 6 E hex

Exercises

You might also like