C Programming - Bitwise Operators - Discussion

Discussion Forum : Bitwise Operators - General Questions (Q.No. 2)
2.
Which bitwise operator is suitable for turning off a particular bit in a number?
&& operator
& operator
|| operator
! operator
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
41 comments Page 4 of 5.

Heena said:   1 decade ago
Because && is a logical operator not a bitwise (bitwise is a operator that takes single bit at a time).

Saurav said:   1 decade ago
But! why will && not work?

Anuradha Sharma said:   1 decade ago
If we take any bit AND (&) with 0 will give a zero. (i.e) Will turn that particular bit OFF.

Ramya said:   1 decade ago
Thanks sundar.

Sundar said:   1 decade ago
@Kumar

Let me explain.

How to turn off only the 4th bit (from right) in a 16-bit binary number?

unsigned int intFalg4Off = 0xfff7;
//Hex = 0xfff7 (or) Decimal = 65527 (or) Binay = 11111111 11110111

unsigned int intInputVal = 255;
//Decimal = 255 (or) 0x00ff (or) 00000000 11111111

unsigned int Result = intInputVal & intFalg4Off;

The result will be the & (AND operation) between the binary numbers given below :

11111111 11110111
00000000 11111111
00000000 11110111 (Decimal = 247 or Hex = 0x00f7)

Here 4th bit of the given input has been turned off. Therefore, intResult will contain the value 247.

Hope this will help you. Have a nice day!

Kumar said:   1 decade ago
Could anyone give an explanation in brief with example the above examples could not be understood

Sohan lal mits gwalior said:   1 decade ago
Bitwise 'AND' of any bit with zero bit is always zero (off).

Arjun Prasad said:   1 decade ago
~(1<<"bit position") is the operand

Arjun Prasad said:   1 decade ago
To make above operand you take a number 1 and then shift its only set bit to the number of the bit you want to turn off(1<<3) and then take AND with given number.

Suhas . u said:   1 decade ago
Which bit u want make 0 ...make only that bit 0 n allother bits 1...
ex:-to make 3rd bit 0 use &operator with one operand as11110111


Post your comments here:

Your comments will be displayed after verification.