Wednesday, July 11, 2007

What is Bitwise operators and how it works....?

Bitwise (Boolean) logic operators


Because microcontrollers store numbers in binary, it is possible to use a special kind of operator on data. These operators are described as Bitwise logic, because they obtain results based on the logical relationships of individual bits. Bitwise logic is also known as Boolean logic or Boolean math.

There are 7 Bitwise operators:

PICBASIC Operator

Description

~

Bitwise NOT

&

Bitwise AND

|

Bitwise OR

^

Bitwise Exclusive OR (XOR)

&/

Bitwise NOT AND (NAND)

|/

Bitwise NOT OR (NOR)

^/

Bitwise NOT Exclusive OR (XNOR)

The simplest of these is the NOT (~) operator. It returns the logical opposite of the tested bit. This operator is unique in that it only requires 1 bit as input. All the other operators require 2 bits.


Using Bitwise operators on individual bits

NOT (~)

When describing the function of Bitwise operations, we use a diagram call a "truth table". It shows the input bit (or bits) on the left, and the result on the right. Since the NOT operator only has one input, the truth table looks like this:

A

~A

0

1

1

0

PICBASIC example:

result = ~ A

AND (&)

AND compares 2 bits and returns logic high only if both input bits are high.

A

B

A & B

0

0

0

0

1

0

1

0

0

1

1

1

PICBASIC example:

result = A & B

OR (|)

OR compares 2 bits and returns a logic high if either or both inputs are high.

A

B

A | B

0

0

0

0

1

1

1

0

1

1

1

1

PICBASIC example:

result = A | B

XOR (^)

XOR compares 2 bits and returns logic high only if a single input is high. If both inputs are high, it returns logic low

A

B

A ^ B

0

0

0

0

1

1

1

0

1

1

1

0

PICBASIC example:

result = A ^ B

NAND (&/)

NAND compares 2 bits and returns logic low if both inputs are high.

A

B

A &/ B

0

0

1

0

1

1

1

0

1

1

1

0

PICBASIC example:

result = A &/ B

NOR (|/)

NOR compares 2 bits and returns logic low if either or both inputs are high.

A

B

A |/ B

0

0

1

0

1

0

1

0

0

1

1

0

PICBASIC example:

result = A |/ B

XNOR (^/)

XNOR compares 2 bits and returns logic low only if a single input is high. If both inputs are high, it returns logic high.

A

B

A ^/ B

0

0

1

0

1

0

1

0

0

1

1

1

PICBASIC example:

result = A ^/ B


Using Bitwise operators on bytes and words

When you use byte or word sized data as inputs for Bitwise operators, the result can be as long as your longest input. The operator will perform a comparison of each bit in both input variables, and store the result in the corresponding bit location of the result variable.

A common example is the AND operator used to mask certain bits in a byte. It works as a mask because it always returns 0 when one of the inputs is 0. Therefore, when we use "bytevar & %00001111", the top 4 bits of the result will always be 0. The lower 4 bits won't change, because ANDing something with logic 1 makes the result equal to the input.

Here's a modified truth table showing 8-bits being ANDed all at once.

bit position

byte A

byte B

A & B

7

1

0

0

6

0

0

0

5

1

0

0

4

1

0

0

3

0

1

0

2

1

1

1

1

1

1

1

0

0

1

0

PICBASIC example:

A = %10110110
B = %00001111
result = A & B

'(00000110)

Here are some example equations using the input values above:

%10110110 | %00001111 = %10111111 'use OR to mask bits with logic 1

%10110110 ^ %00001111 = %10111001 'use XOR to invert selected bits

Let me know your queries to me (Elango C)

1 comment:

Elan said...

Good Job...... Very much useful for the freshers.... especially who go for the interview to the embedded companies.... a must to know these things.... Good