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?
Discussion:
41 comments Page 1 of 5.
Ravi(BIT Mesra CSE_2k7) said:
1 decade ago
Any bit AND(&) with 0 will give a zero .i.e. will turn that particular bit OFF.
(1)
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
ex:-to make 3rd bit 0 use &operator with one operand as11110111
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.
Arjun Prasad said:
1 decade ago
~(1<<"bit position") is the operand
Sohan lal mits gwalior said:
1 decade ago
Bitwise 'AND' of any bit with zero bit is always zero (off).
Kumar said:
1 decade ago
Could anyone give an explanation in brief with example the above examples could not be understood
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!
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!
Ramya said:
1 decade ago
Thanks sundar.
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.
Saurav said:
1 decade ago
But! why will && not work?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers