Operations On Bits

Bits Operation

Operations on Bits : So far we have deal with character, integers, floats and their variations. The smallest element in memory on which we are able to operate  as yet is a byte; and we operated on it by making use of the data type char. However, we haven’t attempted to look within these data types to see how they are constructed out of individual bits, and how these bits can be manipulated. Being able to operate on a bit level, can be very important in programming, especially when a program must interact directly with the hardware. This is because, the programming languages are byte oriented, whereas hardware tends to be bit oriented.


Bit Numbering and Conversion

A bit is the most basic unit of information. It can take a value 0 or 1. 4 bits together from a nibble, 8 bits from a byte, 16 bits from a word and 32 bits from a double-word. Bits are numbered from zero onwards, increasing from right to left as shown below:

7 6 5 4 3 2 1 0

Character

  15    14    13      12     11     10      9       8       7        6        5      4        3       2       1       0

                
16-bit integer

As you might be aware, hexadecimal numbering system each number is built using a combination of digits 0 to 9 and A to F.  Digits A to F are symbols used to represent value 10 to 15. Each hexadecimal digit can be represented using 4-bit nibble as shown below:

HexBinaryHexBinary
0000081000
1000191001
20010A1010
30011B1011
40100C1100
50101D1101
60110E1110
70111F1111

Bits Operations

Here are some examples of operations that we may wish to perform on bits:

i) set bit 3 to 0
ii) set bit 5 to 1
iii) Check whether bit 6 is 1 (on) or 0 (off)

As you can see, in the first two example we are manipulating (writing) a bit, whereas, in the third example we are accessing (reading) a bit. To able to access or manipulate individual bits C language provides a powerful set of bit manipulation operators. These are :

OperatorMeaning
~One’s complement
>> Right shift
<< Left shift
&Bitwise AND
|Bitwise OR
^Bitwise XOR (Exclusive OR)
These operators can operate on int and char but not on floats and doubles.

One’s Complement Operator:

On taking one’s complement of a number, all 1’s present in the number are changed to 0’s and all 0’s are changed to 1’s. For example, one’s complement of 1010 is 0101.

Right Shift Operator:

The right shift operator is represented by >>. It needs two operands. It shifts each bit in its operand to the right.The number of the places the bits are shifted depends on the number  following  the operator (i.e. its right operand).

If the variable ch contains the bit pattern 11010111, then, ch >> 1 would give 01101011 and ch >> 2 would give 00110101.


Left Shift Operator :

The left shift operator (<<) is similar to the right shift operator (>>), the only difference being that the bits are shifted to the left, and for each bit shifted, as 0 is added to the right of the number.


Bitwise AND Operator

This operator is represented as &. Remember it is different than &&, the logical AND operator. The & operator operates on two operands. While operating upon these two operands they are compared on a bit-by-bit basis.Hence both the operands must be of the same type (either char or int). The second operand is often called an AND mask.

First bitSecond bitFirst bit & Second bit
000
010
100
111

Bitwise OR Operator:

Another important bitwise operator is the OR operator which is represented as | .

|01
001
111

Bitwise XOR Operator:

The XOR operator is represented as ^ and is also called an Exclusive OR Operator. The OR operator returns 1, when any one of the two bits or both the bits 1, whereas XOR returns 1 only if one of the two bits is 1.

^01
001
110

Recommended Posts

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *