Posted on Leave a comment

Subtraction using 2’s complement method

S.N0.DecimalSign MagnitudeOne ComplementTwo Complement
00000000000000
11000100010001
22001000100010
33001100110011
44010001000100
55010101010101
66011001100110
77011101110111
8-010001111=(0000)’+1
=1111 + 1
=0000(discard MSB)
9-110011110=(0001)’ + 1
=(1110) + 1
=1111
10-210101101 =(0010)’ + 1
=(1101) + 1
=1110
11-310111100 =(0011)’ + 1
=(1100) + 1
=1101
12-411001011 =(0100)’ + 1
=(1011) + 1
=1100
13-511011010 =(0101)’ + 1
=(1010) + 1
=1011
14-611101001 =(0110)’ + 1
=(1001) + 1
=1010
15-711111000 =(0111)’ + 1
=(1000) + 1
=1001
16-8(not allowed in 4-bit) (not allowed in 4-bit) =(1000)’+1
=(0111)+1
=1000
Table showing Decimal Numbers representation in sign-magnitude, one complement and two complement

Subtraction using 2’s complement method is performed in microcontroller processors.

To perform subtraction using 2’s complement method we need to do the following steps.

step 1: Find the 2’s complement of subtrahend number.

step 2: Add the 2’s complement of subtrahend to the minuend.

step 3: Check the result for the carry.

If there is carry generated.
Then the result obtained is positive. And there is no further processing needed.

If there is no carry generated.
Then the result obtained is negative. And you have to do 2’s complement of the result obtained.

NOTE:
When doing subtraction using 2’s complement. Pay close attention to the sign of the subtrahend and minuend and the result.

EXAMPLE:

Ques.) 5 – 2 = ?
Ans.) 5 – 2 = 3 (in decimal)

using 2’s complement method
(-2) representation in two’s complement form = 1110

(5) in 2’s complement number = 0101

0101
+ 1110
——-
1 0011 = 3 (by discarding the MSB 1, Here MSB is the carry)
There is a carry generated. So, the number is positive and no further processing is required.
We can discard the carry.

Ques.) -8 – 2 = ? in a 4-bit system
Ans.) -8 – 2 = -10 (in decimal)
Remeberwe have only 4 bit to store the number.
using 2’s complement method
(-2) representation in two’s complement form = 1110

(-8) in 2’s complement number = 1000

1000
+ 1110
——-
1 0110 = 6 (by discarding the MSB 1, Here MSB is the carry)
There is a carry generated. So, the number is positive and no further processing is required.
But, the result obtained is wrong. Because if we add two numbers of the same sign result must be of the same sign. This condition creates an overflow.

Overflow in 2’complement subtraction

When two numbers of the same sign are added together and they produce a result with an opposite sign; An overflow has occurred and the result is not valid.

Posted on Leave a comment

How Integer and Floating point numbers are stored in computers

The number has two parts.

  1. Integer Part
  2. Fraction Part

Integers are numbers without the fraction part.
They can range from negative infinity to positive infinity.
Integers are further divided into two parts.

  1. Unsigned number (0 to (2^n – 1); here n = width of data bus)
  2. Signed Numbers ((-2^(n-1)) to (2^n – 1))

Fractional numbers or Floating Point numbers are stored in a standard format known as IEEE 754.
IEEE 754 Floating-Point Standard comes in three different modes.

  1. Single precision Format (32 bits)
  2. Double-precision format


Posted on Leave a comment

Octal Number System

In the octal Number system, we start counting from 0 which goes up to 7. Making only 8 digits. Then after 7, we start by incrementing 1 to the tenth place and start counting from 0 up to 17.

In the video, I have described what the octal number system is and how we can convert the octal number to decimal numbers.

Just like we have fractions in decimals like 1.234, 3.9864 etc. we also have fractions in the octal number system as well like 7741.2225, 12345.654 etc.

Posted on Leave a comment

Binary Number System

In a binary number system, there are only two possible states, HIGH state and LOW state.
These two states can be represented by current levels, voltage levels, bumps and valleys on the surface of CD/DVD etc.

The two states are generally represented by a ‘0’ for LOW state and by a ‘1’ for the HIGH state.

Using the combination of 0’s(LOW) and 1’s(HIGH), we can represent the other number systems such as hexadecimal, octal or decimal into
binary number system.

Bit Nibble Byte Word Dword Qword