# C – Bit-wise Operator Bit wise Operators (Can only used on integrals, no floats and doubles)

# Bit-Wise Logic Operator

• & AND
• | OR
• ^ XOR
• ~ ONE’S COMPLEMENT (Unary Operator)

& AND

| OR

^ XOR

~ ONE’S COMPLEMENT (Unary Operator)

# Bit-wise Shift

• << LEFT SHIFT
• >> RIGHT SHIFT
• Example : A = 0101 1000 >>1 Output : 000010000 .
• Reference Videos:
1. Special Operators

# Exercise

Ans : 4

## 2) Predict the output of following program.

c= 2

Bitwise AND (&) operator copies bit(s), if they are exist both of the operands. Here, binary of a is “1010” and binary of b is “0010”. Thus, result of expression (a & b) is “0010” which is equivalent to 2 in Decimal.

### 3) Predict the output of following program.

I have purchased …:Mobile, Lappy

Bitwise OR (|) operator copies bit(s), if they are exist either side of the operands (that means if any bit is exist in any operand). Here, binary of Macro MOBILE (0x01) is “0001” and binary of Macro LAPPY (0x02) is “0010”, then result of the expression item |=MOBILE; will be “0001” and second expression item |=LAPPY; will return “0011”. Thus, both conditions (item & MOBILE) and (item & LAPPY) will be true.

## 4) Predict the output of following program.

4,5

Value of var is 0x04 (0100), Consider the expression var = var | 0x04 The OR (|) of 0100, 0100 is 0100, hence value will remain 0100. After the expression var |=0x01, value will be 0101 that is 0x05.

## 5) Predict the output of following program.

13

Consider the expression flag &= ~0x02 => flag = flag & (~0x02) => flag = 0x0f & (~0x02) => flag = D => flag =13.

## 6) Consider the given statement:

int x = 10 ^ 2What will be the value of x?

8

XOR operator (^) copies bit(s), if one operand has 1 and other has 0, consider the given truth table:

Here, binary of 10 is “1010” and binary of 2 is “0010”, then the result of statement (10 ^ 2) will be “1000”, which is equivalent to 8 in decimal.

## 7) Predict the output of following program.

x= 8

The statement x &= ~2; will clear second bit from the value of 10, binary of x is “1010” and the binary of 2 is “0010”, thus this statement will clear second bit and returns “1000” that is equivalent to 8 in Decimal.

## 8) Which Bitwise Operator can be used to check whether a number is EVEN or ODD quickly?

Bitwise AND (&)

Bitwise AND (&) Operator can be used to check whether a number if EVEN or ODD, consider the statement (num & 1), this statement will return 1 if first bit of the number is High (1) else it will return 0. All ODD numbers have their firs bit 1 and ODD numbers have 0.

Consider the following program:

Output

## 9) Which statement is suitable to check 3rd (count from 0) bit is high (set) or not?

Both (1) and (2)

The value of (1<<3) is 8 in Decimal and value of 0x08 is 8 in Decimal, both statements are suitable to check whether 3rd bit of num is High (set) or not.

Consider this program:

Output

Binary of 15 is: 1111 & Binary of 7 is: 0111, thus in first case 3rd bit is high and in second case 3rd bit is low. [Count from 0]

## 10) Left shift (<<) and Right shift (>>) operators are equivalent to _____________ by 2.

Choose the correct words…

Multiplication and Division

Left shift by 1 return the multiplication by 2 and Right shift by 1 return the division by 2.

Consider this program:

Output

This site uses Akismet to reduce spam. Learn how your comment data is processed.

if you have any queries call us +918300026060 or WhatsApp Dismiss

0