Tuesday, January 25, 2011

AND - XOR Effect

Suppose you have an 8-bit binary number and you want to find 1's complement of it:

(I am taking assumption to explain the method, this method works well with any binary representation of the number)

Assumption: Let's say the 8-bit number is 10011110.

Method-

1 0 0 1 1 1 1 0
1 1 1 1 1 1 1 1 Apply XOR of the 2 numbers
--------------
0 1 1 0 0 0 01

So, we are able to find a 1's complement of the number 10011110 with a single XOR function as shown in this simple example.

This method works for any binary number representation.

Suppose you have an 8-bit binary number and you want to check whether the number is non-zero or not:

(I am taking assumption to explain the method, this method works well with any binary representation of the number)

Assumption: Let's say the 8-bit number is 10011110.

Method-

1 0 0 1 1 1 1 0
1 1 1 1 1 1 1 1 Apply AND of the 2 numbers
--------------
1 0 0 1 1 1 1 0

So, we are able to find a number 10011110, from which we can say that the number is non-zero, with a single AND function as shown in this simple example.

This method works for any binary number representation.

However, this method is useful to check for a single bit whether it is zero or not.

As an example:

We will check for the input 10011110, whether the bit at position 3 is '0' or not. (considering the Least significant bit as 1st bit)

10 0 1 1 1 1 0
000 00 1 0 0 Apply AND of the 2 numbers
--------------
000 00 1 0 0

Here, the result is >0. Therefore,
we can say that for a number 10011110, the bit at position 3 (
10011110) is 1.

In general, to check the nth bit for the number, keep the nth bit as 1 and reset the rest of the bits in the input number, with which you are going to perform AND operation.

No comments: