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.
Nag raj said:
3 years ago
Bit-wise AND operator is able to mask a particular bit.
Eg: Let us take 15 (decimal).
--->1111 (binary).
If we want 3rd bit only then simply mask the given number with 1000 by using bit-wise AND.
1111 (given number).
1000 (3rd-bit mask value).
---- - (bit-wise AND operation).
1000 (required result).
Eg: Let us take 15 (decimal).
--->1111 (binary).
If we want 3rd bit only then simply mask the given number with 1000 by using bit-wise AND.
1111 (given number).
1000 (3rd-bit mask value).
---- - (bit-wise AND operation).
1000 (required result).
(7)
Anusha said:
8 years ago
! operator makes the non zero number into zero.
thus (!52)=0,!65=0,...........
but ! (0)=1
why don't the answer is ! operator?
Can anyone explain?
thus (!52)=0,!65=0,...........
but ! (0)=1
why don't the answer is ! operator?
Can anyone explain?
(5)
Ruhi said:
7 years ago
Thanks all for explaining this.
(3)
Gafoor said:
8 years ago
how to work & operator in the program?
Can anyone explain with an example, please?
Can anyone explain with an example, please?
(2)
Deepak_Bboy said:
10 years ago
AND.
0 & 0 = 0 OFF.
1 & 0 = 0 OFF.
OR.
X & 0 = 0 OFF.
OR.
Input & 0 = 0 OFF.
BUT OR.
0| 0 = 0.
0| 1 = 1.
1| 0 = 1.
1| 1 = 1.
You can't OFF a 1 (input).
As you can see above.
0 & 0 = 0 OFF.
1 & 0 = 0 OFF.
OR.
X & 0 = 0 OFF.
OR.
Input & 0 = 0 OFF.
BUT OR.
0| 0 = 0.
0| 1 = 1.
1| 0 = 1.
1| 1 = 1.
You can't OFF a 1 (input).
As you can see above.
(2)
Prema Latha.S said:
1 decade ago
Bitwise AND operator (&), one's complement operator(~)
Example: To unset the 4th bit of byte_data or to turn off a particular bit in a number.
Explanation: Consider, Material from Interview Mantra. Subscribe to free updates via email.
char byte_data= 0b00010111;byte_data= (byte_data)&(~(1<<4));
1 can be represented in binary as 0b00000001 = (1<<4)
<< is a left bit shift operator,
It shifts the bit 1 by 4 places towards left.
(1<<4) becomes 0b00010000
And ~ is the one's complement operator in C language.
So ~(1<<4) = complement of 0b00010000
= 0b11101111
Replacing value of byte_data and ~(1<<4) in (byte_data)&(~(1<<4));
We get (0b00010111) & (0b11101111)
Perform AND operation to below bytes.
00010111
11101111
-----------
00000111
-----------
Thus the 4th bit is unset.
Example: To unset the 4th bit of byte_data or to turn off a particular bit in a number.
Explanation: Consider, Material from Interview Mantra. Subscribe to free updates via email.
char byte_data= 0b00010111;byte_data= (byte_data)&(~(1<<4));
1 can be represented in binary as 0b00000001 = (1<<4)
<< is a left bit shift operator,
It shifts the bit 1 by 4 places towards left.
(1<<4) becomes 0b00010000
And ~ is the one's complement operator in C language.
So ~(1<<4) = complement of 0b00010000
= 0b11101111
Replacing value of byte_data and ~(1<<4) in (byte_data)&(~(1<<4));
We get (0b00010111) & (0b11101111)
Perform AND operation to below bytes.
00010111
11101111
-----------
00000111
-----------
Thus the 4th bit is unset.
(1)
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)
Simanta said:
10 years ago
For an example:
int a,b,c;
a=5;
b=6;
c=a&&b;
//In this situation when a and b having any -ve or +ve value.
Then it is taken as 1. So c = 1 && 1=1. Rather then '0' all value are taken as 1.
d = a & b; //In this situation the operation is perform between every binary bit of a and b. So a = 5 = 101.
b = 6 = 110 so d = 100 = 4.
That is why '&&' called logical operator and '&' called bit-wise operator.
int a,b,c;
a=5;
b=6;
c=a&&b;
//In this situation when a and b having any -ve or +ve value.
Then it is taken as 1. So c = 1 && 1=1. Rather then '0' all value are taken as 1.
d = a & b; //In this situation the operation is perform between every binary bit of a and b. So a = 5 = 101.
b = 6 = 110 so d = 100 = 4.
That is why '&&' called logical operator and '&' called bit-wise operator.
(1)
Pritam said:
9 years ago
If we read question carefully what I understood is that if I want to turn off the bit.
i.e First bit is on means 1. To turn off it I use negation so that it will turn off.
i.e First bit is on means 1. To turn off it I use negation so that it will turn off.
(1)
Emanuel said:
9 years ago
I think ^ operator.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers