C Programming - Bitwise Operators - Discussion
Discussion Forum : Bitwise Operators - General Questions (Q.No. 4)
4.
Which bitwise operator is suitable for checking whether a particular bit is on or off?
Discussion:
29 comments Page 1 of 3.
Vykuntapu gopi said:
8 years ago
The output of logical operators gives 0/1.
The output of bitwise operators gives decimal of given numbers.
Example:
(a=5)&&(b=2)
the a&b are both are non-zero numbers it gives result as 1
if
(a=5)&&(b=0)
So result is 0.
Because one of operands b is zero.
But in bitwise operators, the operation goes on the binary number of given operands.
if
(a=2)&(b=1)
convert the 2,1 binary numbers in 4 bits that 8421;
2's binary number 0010
1's binary number is 0001
0&0=0
0&0=0
1&0=0
0&1=0
Output is 0
The output of bitwise operators gives decimal of given numbers.
Example:
(a=5)&&(b=2)
the a&b are both are non-zero numbers it gives result as 1
if
(a=5)&&(b=0)
So result is 0.
Because one of operands b is zero.
But in bitwise operators, the operation goes on the binary number of given operands.
if
(a=2)&(b=1)
convert the 2,1 binary numbers in 4 bits that 8421;
2's binary number 0010
1's binary number is 0001
0&0=0
0&0=0
1&0=0
0&1=0
Output is 0
Seenu said:
1 decade ago
Hi! Guys this is a simple one. Let consider 8-bit binary number,
Binary(8 bit) = 0000 1100
suppose we want check 3rd bit is on or off(on=1 & off=0), just like this,
0000 1100 (Decimal=12)
&
0000 0100 (Decimal=4,we are checking 3rd bit so it is 1)
----------
0000 0100 (Decimal=4)
----------
If we got some value then checking bit is on,otherwise off.
In C language,
if(12&4)
print("The third bit is on");
else
print("The third bit is off");
Binary(8 bit) = 0000 1100
suppose we want check 3rd bit is on or off(on=1 & off=0), just like this,
0000 1100 (Decimal=12)
&
0000 0100 (Decimal=4,we are checking 3rd bit so it is 1)
----------
0000 0100 (Decimal=4)
----------
If we got some value then checking bit is on,otherwise off.
In C language,
if(12&4)
print("The third bit is on");
else
print("The third bit is off");
Love shukla said:
1 decade ago
&& is a Logical operator which combine two or more relation
Ex: let a=5,b=2
(a==5)&& (b==2)
true && true = true
i.e, 1 && 1 = 1
While & is a bitwise operator which has ability to support the manipulation of data at bit level
Ex:let a=9,b=15
Then, a & b
9 & 15
1001 & 1111 = 1001 (use AND operation on each bit)
i.e, ans:- 9
Ex: let a=5,b=2
(a==5)&& (b==2)
true && true = true
i.e, 1 && 1 = 1
While & is a bitwise operator which has ability to support the manipulation of data at bit level
Ex:let a=9,b=15
Then, a & b
9 & 15
1001 & 1111 = 1001 (use AND operation on each bit)
i.e, ans:- 9
Madhureddy said:
1 decade ago
Hai frndz. How & operator is used to check whether a bit is on or off is as follows let us conceive that there is a binary num 10101000 if you want to check third bit is on or off then perform and operation to 00001000 then if 3rd bit is 1 its on else its turned off.
Here if wana check nth bit make n-1 bit as 1 remaining as 0. Then perform and operation then you will know the answer.
Here if wana check nth bit make n-1 bit as 1 remaining as 0. Then perform and operation then you will know the answer.
Saikat Sinha Ray said:
1 decade ago
& is a bit wise operator that performs the AND operation of binary representation of given numbers.
Ex : 2 && 0 = 0 (In binary 0000 0010 & 0000 0000 = 00000 0000)
&& is a logical operator that matches between two condition
Ex : if(condition1 is true && condition2 is true)
................
else
................
Ex : 2 && 0 = 0 (In binary 0000 0010 & 0000 0000 = 00000 0000)
&& is a logical operator that matches between two condition
Ex : if(condition1 is true && condition2 is true)
................
else
................
ULLAS KULKARNI said:
1 decade ago
In detail,,
The key difference between & and && is ,its return value
EX; 1&2 returns zero whereas 1&&2 returns one ..
check out
1=0001 & 0010= 0000 on & opertion
and 1(true) and 2(true) so 1&& 2=1(true)
The key difference between & and && is ,its return value
EX; 1&2 returns zero whereas 1&&2 returns one ..
check out
1=0001 & 0010= 0000 on & opertion
and 1(true) and 2(true) so 1&& 2=1(true)
Bit_ter said:
1 decade ago
@Bhavin the answer is & because,
0 | 1 =1
1 | 1 =1.
But,
0 & 1 =0
1 & 1 =1.
You can see that when you '&' a particular bit (0 or 1) with 1 then the output is the input.
0 | 1 =1
1 | 1 =1.
But,
0 & 1 =0
1 & 1 =1.
You can see that when you '&' a particular bit (0 or 1) with 1 then the output is the input.
Harsha said:
4 years ago
I am not thinking that it is a true answer.
There is no logic and bitwise & binary operator it takes min 2 operands, so how it check that whether a particular bit is on or off.
There is no logic and bitwise & binary operator it takes min 2 operands, so how it check that whether a particular bit is on or off.
Anusha said:
1 decade ago
Yeah, & is the correct answer to turnoff the bit in a number.
The basic operations of & is :
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1.
The basic operations of & is :
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1.
Kishore said:
1 decade ago
As we know that truth table of AND is if 2 number is true then only output becomes true. For that reason & is used to find bits on or off.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers