Tuesday, January 31, 2012

Compliment representation of numbers

In sign magnitude system the sign of a binary number is denoted by a extra bit in-front of the number.

For example +5 is denoted by 0,101.

0 : show that the number is positive
1: show that the number is negative

Ones compliment

In ones compliment system the representation of positive number is the same as that of sign magnitude system. But the representation of negative number is different.

For example -5 is represented in ones compliment as 1,010. This is obtained by replacing all the 1s by 0 and zero by 1 in the binary representation of +5 which is 0,101.

The process of replacing a 0 by 1 and 1 by 0 is called bits complimenting.

In general a n bit number x's (with out sign bit) ones compliment is given by (2^n-1-x).

Twos compliment

The twos compliment is obtained by adding 1 to the ones compliment of a number.

Example:

+5=0,101
-5=1,010 (1s compliment)

now twos compliment of -5 is 1,010 + 0001 = 1,011

In general for an n bit number twos compliment is given by 1,(2^n-x). Another method to to find the twos compliment of a binary number is to scan the number from left to right and compliment all the bits appearing after the first one. An example I given below.

0,1110 → 1,0010

Friday, January 27, 2012

Binary subtraction


Binary subtraction

Before explaining subtraction lets see how negative numbers are represented in binary. To denote the sign of a binary number a extra bit is used in the left end of the number. By convention zero is used to represent + sign and a one is used to represent -sign.

+5 = 0,101
-7 = 1,111

Subtraction is the process of adding a negative number to a positive number. If a number having a positive sign is added to a number with a number y having a negative sign ,it is equivalent to subtracting y from x.

Consider the example below
101-011

Borrow
1
0

Minuend
1
0
1
Subtrahend
0
1
1
Difference
0
1
0


Borrow
0
Minuend
0
Subtrahend
0
Difference
0

Borrow
1
Minuend
0
Subtrahend
1
Difference
1

From the above examples we can construct a half subtracter table and full subtracter table.



Half subtracter

a
b                                       Difference             Borrow
0
0
0
0
0
1
1
1
1
0
1
0
1
1
0
0

Full subtracter

A(Minuend)
B(Subtrahend)
Borrow
Difference
Borrow to next position
0
0
0
0
0
0
0
1
1
1
0
1
0
1
1
0
1
1
0
1
1
0
0
1
0
1
0
1
0
0
1
1
0
0
0
1
1
1
1
1







Addition of binary numbers


Binary addition

Counting is the process of adding 1 to the present number to get the next number. In decimal number system counting start from 0 and proceeds like this 0,1,2,3,4,5,6,7,8,9 . After nine we have no more symbols left, so we write 10. This one represent a carry to the tens position.

Like this binary system's count process can also be represented like this 0,1,10,11,100 ….

Based on the above idea we construct a half adder table to represent the addition of binary numbers.

a
 b                                              Sum                           Carry
0
0
0
0
0
1
1
0
1
0
1
0
1
1
1
1


Here we have not used carry , but consider the examples below.

11+01=100

Carry
1
1

Augend

1
1
Addend

0
1
Sum
1
0
0

101+001=110
Carry
0
1

Augend
1
0
1
Addend
0
0
1
Sum
1
1
0

These two examples show that addition between two numbers include addition between three bits; the carry bit and the bits of the two numbers to be added. We can construct a addition table having these three values.

a
              b                             Carry                     Sum       Carry to next position
0
0
0
0
0
0
0
1
1
0
0
1
0
1
0
0
1
1
0
1
1
0
0
1
0
1
0
1
0
1
1
1
0
0
1
1
1
1
1
1




In short rules for addition are

0+0=0
1+0=1
0+1=1
1+1=0 , and a carry to the next significant digit





Thursday, January 26, 2012

BCD numbers


Binary coded decimal numbers

This is the method of representing decimal numbers using binary digits.

There are 10 symbols in decimal number system , each of these symbols is represented by a unique string consisting of two symbols of binary number system. A four bit binary digit is used for each decimal symbol because with three bit binary digit we can only represent 2^3 =8 digits.

Eg Decimal number 15 if converted to binary becomes 1111. But when 15 is encoded binary it becomes 0001 0101 . 0001 representing 1 and 0101 representing 5.

The look up table for encoding decimal number is shown below.

Decimal digit
Binary code
0
0000
1
0001
2
0010
3
0011
4
0100
5
0101
6
0110
7
0111
8
1000
9
1001


Encoding to binary is faster than conversion to binary because it is just plain look up to the conversion table and replacing, while conversion involves repeated division which make the
it slow. On an average log(10)2 =3.32 bits are required when decimal numbers are converted to binary. 4 bit s are needed when decimal is encoded. The (4/3.32)=1.2 is the measure of the extra bits needed to store encoded numbers.

We need at-least 4 bits to represent a decimal digit. But there is 16 four bit groups and we need only 10 of these 16 for encoding decimal digits. There is 16!/6! permutations of selecting 10 out of 16 items. Most of the possible 3*10^10 codes are not useful, only a small numbers is chosen are used and chosen according to ease in arithmetic, error correction properties, ease in coding etc. The useful code are divided into four classes .They are

1.Weighted codes
2.Self complementing codes
3.Cyclic,reflected or Gray codes


Weighted codes

Weighted code examples

Decimal Digit
Weights
Weights
Weights


8 4 2 1
8 4 -2 -1
2 4 2 1
0
0 0 0 0
0 0 0 0
0 0 0 0
1
0 0 0 1
0 1 1 1
0 0 0 1
2
0 0 1 0
0 1 1 0
0 0 1 0
3
0 0 1 1
0 1 0 1
0 0 1 1
4
0 1 0 0
0 1 0 0
0 1 0 0
5
0 1 0 1
1 0 1 1
1 0 1 1
6
0 1 1 0
1 0 1 0
1 1 0 0
7
0 1 1 1
1 0 0 1
1 1 0 1
8
1 0 0 0
1 0 0 0
1 1 1 0
9
1 0 0 1
1 1 1 1
1 1 1 1


As you can see from the table the decimal value of the code is the algebraic sum of the weights of the columns in which 1 appears.

d=w(i)b(i)

I shall explain this with the help of and example.

7=0*8 + 1*4 + 1*2 + 1*1
7=1*8 +0*4 + 0 *-2+ 1*-1
7=1*2+ 1*4+ 0*2 + 1*1

The weights may be negative, negative weights can be represented by a over bar or by a – sign. The same weights can repeated twice like in (2,4,2,1) code. The weights should be chosen in such a way that it they should be able to represent all the digits from 0-9.

The first 10 groups of 4 bits represent 0 to 9, the remaining 6 are illegal combinations and may be used for error detection.

Self-complementing codes

Here the code is made in such a manner that if we replace the 0s by 1 and 1s by zero we get the code for 9-d. The table below shows some self complimenting codes.

d
Code for d
Code for 9-d
9-d
2421
2421
0
0000
1111
9
1
0001
1110
8
2
0010
1101
7
3
0011
1100
6
4
0100
1011
5
5
1011
0100
4
6
1100
0011
3
7
1101
0010
2
8
1110
0001
1
9
1111
0000
0

A condition for self-complementing code is that the sum should be be nine(2+4+2+1=9).

There are self complimenting codes which are not weighted, excess three code is an example. For making a excess three code the number is taken in BCD form first . Then binary three(4 bit) is added to every four bit groups .

Eg find the excess three code of 739.

BCD code of 739= 0111 0011 1001 .

Adding binary 3(0011) to every four bit groups of the BCD representation of 739

0111 0011 1001
+0011 0011 0011
---------------------
1010 0110 1100

So excess three code for 739 is 1010 0110 1100.


Gray code,Reflected codes or Cyclic codes.

Hamming distance between two equal length binary sequences is the number of positions they differ in. Eg A= 0110 B=1010 , the hamming distance between them 2.
In cyclic code the adjacent code groups differ from each other by only one bit. Hamming distance between two successive code group in a cyclic code is unity.

The counting sequences in base-10, binary and Gray codes go as shown below.

Base-10
Binary
Gray
0
0000
0000
1
0001
0001
2
0010
0011
3
0011
0010
4
0100
0110
5
0101
0111
6
0110
0101
7
0111
0100
8
1000
1100
9
1001
1101

In each subsequent number in gray code only the lowest bit possible is changed.