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

Binary Number System

Dr. Odelu Vanga

Department of Computer Science and Information Systems


Birla Institute of Technology and Science Pilani
Hyderabad Campus
odelu.vanga@hyderabad.bits-pilani.ac.in

January 17, 2020

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 1 / 29


Today’s Topics

Decimal Number System (base-10)

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 2 / 29


Today’s Topics

Decimal Number System (base-10)

Binary Number System (base-2)

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 2 / 29


Today’s Topics

Decimal Number System (base-10)

Binary Number System (base-2)

Binary Addition and Subtraction

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 2 / 29


Today’s Topics

Decimal Number System (base-10)

Binary Number System (base-2)

Binary Addition and Subtraction

Fixed Precision and Overflow

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 2 / 29


Today’s Topics

Decimal Number System (base-10)

Binary Number System (base-2)

Binary Addition and Subtraction

Fixed Precision and Overflow

Sign and Magnitude

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 2 / 29


Today’s Topics

Decimal Number System (base-10)

Binary Number System (base-2)

Binary Addition and Subtraction

Fixed Precision and Overflow

Sign and Magnitude

One’s complement

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 2 / 29


Today’s Topics

Decimal Number System (base-10)

Binary Number System (base-2)

Binary Addition and Subtraction

Fixed Precision and Overflow

Sign and Magnitude

One’s complement

Two’s complement

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 2 / 29


Decimal and Binary Representation
Decimal number (base-10)

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 3 / 29


Decimal and Binary Representation
Decimal number (base-10)

Binary number (base-2)

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 3 / 29


Converting Decimal to Binary Number

Binary equivalent for decimal number 35

MSD - most significant digit


LSD - least significant digit

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 4 / 29


Converting Decimal to Binary Number

Binary equivalent for decimal number 35

MSD - most significant digit


LSD - least significant digit

(35)10 = (100011)2 or
simply write 100011

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 4 / 29


Converting Decimal to Binary Number

Binary equivalent for decimal number 35

MSD - most significant digit


LSD - least significant digit

(35)10 = (100011)2 or
simply write 100011

Could you Develop Algorithm ?


Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 4 / 29
Decimal to Binary Number

Algorithm
Start
1. Read x
2. Process q = x/2 (Note x/2 returns the quotient)
3. Process r = x%2 (Note x%2 returns the remainder)
4. Store r

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 5 / 29


Decimal to Binary Number

Algorithm
Start
1. Read x
2. Process q = x/2 (Note x/2 returns the quotient)
3. Process r = x%2 (Note x%2 returns the remainder)
4. Store r
5. if (q = 0) then Goto Step7
6. else
6.1 Assign x = q
6.2 Goto Step2
7. print r values from last to fist.
Stop

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 5 / 29


Converting Binary to Decimal Number
Convert binary number 1010 to decimal equivalent

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 6 / 29


Converting Binary to Decimal Number
Convert binary number 1010 to decimal equivalent

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 6 / 29


Converting Binary to Decimal Number

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 7 / 29


Converting Binary to Decimal Number

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 8 / 29


Converting Binary to Decimal Number

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 9 / 29


Converting Binary to Decimal Number

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 10 / 29


Converting Binary to Decimal Number

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 11 / 29


Binary Addition

Add the binary numbers 1010 and 11

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 12 / 29


Binary Addition

Add the binary numbers 1010 and 11


Conceptually similar to
decimal addition
Addition rules:

0+0 = 0
0+1 = 1
1+0 = 1
1 + 1 = 10

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 12 / 29


Binary Addition

Add the binary numbers 1010 and 11


Conceptually similar to
Example (Add the binary
decimal addition
numbers 1010 and 11)
Addition rules:

0+0 = 0
0+1 = 1
1+0 = 1
1 + 1 = 10

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 12 / 29


Binary Subtraction

Conceptually similar to
decimal addition
Addition rules:

0−0 = 0
0 − 1 = 1 barrow 1
1−0 = 1
1−1 = 0

Barrow 1 is equivalent to 2

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 13 / 29


Binary Subtraction

Conceptually similar to
decimal addition Example (Subtract binary
Addition rules:
number 101 from 1011)

0−0 = 0
0 − 1 = 1 barrow 1
1−0 = 1
1−1 = 0

Barrow 1 is equivalent to 2

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 13 / 29


Binary Subtraction

Conceptually similar to
decimal addition Example (Subtract binary
Addition rules:
number 101 from 1011)

0−0 = 0
0 − 1 = 1 barrow 1
1−0 = 1
1−1 = 0

Barrow 1 is equivalent to 2

How you will subtract larger number from smaller number ?

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 13 / 29


Fixed Precision and Overflow

In computers, numbers represented using a fixed number of bits.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 14 / 29


Fixed Precision and Overflow

In computers, numbers represented using a fixed number of bits.

Typical sizes: 8 bits, 16 bits, 32 bits, 64 bits and 80 bits.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 14 / 29


Fixed Precision and Overflow

In computers, numbers represented using a fixed number of bits.

Typical sizes: 8 bits, 16 bits, 32 bits, 64 bits and 80 bits.

Numbers in which a specific number of bits are used to represent


the value are called fixed precision numbers.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 14 / 29


Fixed Precision and Overflow

In computers, numbers represented using a fixed number of bits.

Typical sizes: 8 bits, 16 bits, 32 bits, 64 bits and 80 bits.

Numbers in which a specific number of bits are used to represent


the value are called fixed precision numbers.

An overflow occurs when the result of a calculation can not be


represented with the number of bits available.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 14 / 29


Fixed Precision and Overflow

Example (Consider 8 bit number)


There are 28 = 256 possible combinations, that is, we can
represent 256 distinct numeric values.

The range considered to be 0 − 255.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 15 / 29


Fixed Precision and Overflow

Example (Consider 8 bit number)


There are 28 = 256 possible combinations, that is, we can
represent 256 distinct numeric values.

The range considered to be 0 − 255.


Any number larger than 255 can not be represented using 8 bits.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 15 / 29


Fixed Precision and Overflow

Example (Consider 8 bit number)


There are 28 = 256 possible combinations, that is, we can
represent 256 distinct numeric values.

The range considered to be 0 − 255.


Any number larger than 255 can not be represented using 8 bits.

Similarly, 16 bits allows a range of 0-65535, and so on.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 15 / 29


Fixed Precision and Overflow

Example (Consider 8 bit number)


There are 28 = 256 possible combinations, that is, we can
represent 256 distinct numeric values.

The range considered to be 0 − 255.


Any number larger than 255 can not be represented using 8 bits.

Similarly, 16 bits allows a range of 0-65535, and so on.

200 + 200 is 400 - out of range of 8 bit number. overflow occur

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 15 / 29


Negative Binary Numbers

When a fixed precision binary number is used to hold only


positive values, it is said to be unsigned.
Range is 0 to 2n − 1, where n - number of bits used

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 16 / 29


Negative Binary Numbers

When a fixed precision binary number is used to hold only


positive values, it is said to be unsigned.
Range is 0 to 2n − 1, where n - number of bits used
It is also possible to represent signed (negative as well as
positive) numbers in binary.
Range is −(2n−1 ) to (2n−1 − 1), where n - number of bits
used

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 16 / 29


Negative Binary Numbers

When a fixed precision binary number is used to hold only


positive values, it is said to be unsigned.
Range is 0 to 2n − 1, where n - number of bits used
It is also possible to represent signed (negative as well as
positive) numbers in binary.
Range is −(2n−1 ) to (2n−1 − 1), where n - number of bits
used
Range of 8-bit unsigned integer - 0 to 255
signed integer −128 to 127.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 16 / 29


Negative Binary Numbers

When a fixed precision binary number is used to hold only


positive values, it is said to be unsigned.
Range is 0 to 2n − 1, where n - number of bits used
It is also possible to represent signed (negative as well as
positive) numbers in binary.
Range is −(2n−1 ) to (2n−1 − 1), where n - number of bits
used
Range of 8-bit unsigned integer - 0 to 255
signed integer −128 to 127.
How do we handle negative numbers ?

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 16 / 29


Negative Binary Numbers

When a fixed precision binary number is used to hold only


positive values, it is said to be unsigned.
Range is 0 to 2n − 1, where n - number of bits used
It is also possible to represent signed (negative as well as
positive) numbers in binary.
Range is −(2n−1 ) to (2n−1 − 1), where n - number of bits
used
Range of 8-bit unsigned integer - 0 to 255
signed integer −128 to 127.
How do we handle negative numbers ?
You might be tempted to reply: Easy, just use a negative sign!.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 16 / 29


Negative Binary Numbers

When a fixed precision binary number is used to hold only


positive values, it is said to be unsigned.
Range is 0 to 2n − 1, where n - number of bits used
It is also possible to represent signed (negative as well as
positive) numbers in binary.
Range is −(2n−1 ) to (2n−1 − 1), where n - number of bits
used
Range of 8-bit unsigned integer - 0 to 255
signed integer −128 to 127.
How do we handle negative numbers ?
You might be tempted to reply: Easy, just use a negative sign!.
But this won’t work;

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 16 / 29


Negative Binary Numbers

When a fixed precision binary number is used to hold only


positive values, it is said to be unsigned.
Range is 0 to 2n − 1, where n - number of bits used
It is also possible to represent signed (negative as well as
positive) numbers in binary.
Range is −(2n−1 ) to (2n−1 − 1), where n - number of bits
used
Range of 8-bit unsigned integer - 0 to 255
signed integer −128 to 127.
How do we handle negative numbers ?
You might be tempted to reply: Easy, just use a negative sign!.
But this won’t work;
because everything must be represented as bits.
Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 16 / 29
Signed Magnitude
Sign and Magnitude:
The leftmost bit represents the sign

0 for positive and 1 for negative.

The remaining bits represent the magnitude.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 17 / 29


Signed Magnitude
Sign and Magnitude:
The leftmost bit represents the sign

0 for positive and 1 for negative.

The remaining bits represent the magnitude.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 17 / 29


Sign and Magnitude
Decimal Binary S&M Remark
0 0000 1000 -0 (-8)
1 0001 1001 -1
2 0010 1010 -2
3 0011 1011 -3
4 0100 1100 -4
5 0101 1101 -5
6 0110 1110 -6
7 0111 1111 -7

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 18 / 29


Sign and Magnitude
Decimal Binary S&M Remark
0 0000 1000 -0 (-8)
1 0001 1001 -1
2 0010 1010 -2
3 0011 1011 -3
4 0100 1100 -4
5 0101 1101 -5
6 0110 1110 -6
7 0111 1111 -7

Example (Sign-Magnitude)
we represent 6 as 0110 and −1 as 1001.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 18 / 29


Sign and Magnitude
Decimal Binary S&M Remark
0 0000 1000 -0 (-8)
1 0001 1001 -1
2 0010 1010 -2
3 0011 1011 -3
4 0100 1100 -4
5 0101 1101 -5
6 0110 1110 -6
7 0111 1111 -7

Example (Sign-Magnitude)
we represent 6 as 0110 and −1 as 1001.
But, we can see the following:
6 + (−1) = 5
But, 0110 + 1001 = 1111, that is, −7

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 18 / 29


Sign and Magnitude
Decimal Binary S&M Remark
0 0000 1000 -0 (-8)
1 0001 1001 -1
2 0010 1010 -2
3 0011 1011 -3
4 0100 1100 -4
5 0101 1101 -5
6 0110 1110 -6
7 0111 1111 -7

Example (Sign-Magnitude)
we represent 6 as 0110 and −1 as 1001.
But, we can see the following:
6 + (−1) = 5
But, 0110 + 1001 = 1111, that is, −7
So, addition is not straightforward if the signs of the two numbers are
different.
Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 18 / 29
One’s complement
Flipping the bits

Decimal Binary 1’s complement Remark


0 0000 1111 -0 (-8)
1 0001 1110 -1
2 0010 1101 -2
3 0011 1100 -3
4 0100 1011 -4
5 0101 1010 -5
6 0110 1001 -6
7 0111 1000 -7

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 19 / 29


One’s complement
Flipping the bits

Decimal Binary 1’s complement Remark


0 0000 1111 -0 (-8)
1 0001 1110 -1
2 0010 1101 -2
3 0011 1100 -3
4 0100 1011 -4
5 0101 1010 -5
6 0110 1001 -6
7 0111 1000 -7

Example
For example, one’s complement of 0110(6) is 1001(−6).
6 + (−6) = 0
But, 0110 + 1001 = 1111 is equivalent to (−8)
Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 19 / 29
Two’s complement

2s complement representation - widely used in microprocessors.


Represents sign and magnitude

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 20 / 29


Two’s complement

2s complement representation - widely used in microprocessors.


Represents sign and magnitude

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 20 / 29


Two’s complement

Converting positive numbers to two’s complement


Same as converting to binary
Converting negative numbers to two’s complement

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 21 / 29


Two’s complement

Converting positive numbers to two’s complement


Same as converting to binary
Converting negative numbers to two’s complement

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 22 / 29


Two’s complement

Converting positive numbers to two’s complement


Same as converting to binary
Converting negative numbers to two’s complement

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 23 / 29


Two’s complement

Converting positive numbers to two’s complement


Same as converting to binary
Converting negative numbers to two’s complement

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 24 / 29


Two’s complement

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 25 / 29


Two’s complement

We can see,
4 + (−4) = 0
0100 + 1100 = 0000

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 25 / 29


Two’s complement
2s complement notation makes it possible to
add and subtract signed numbers

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 26 / 29


Two’s complement
2s complement notation makes it possible to
add and subtract signed numbers

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 27 / 29


Sign and Magnitude

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 28 / 29


.

Next class..
Floating point data representation
Hexadecimal and Octal Number System

Thank You

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 17, 2020 29 / 29

You might also like