C Programming - Bitwise Operators - Discussion

2. 

Which bitwise operator is suitable for turning off a particular bit in a number?

[A]. && operator
[B]. & operator
[C]. || operator
[D]. ! operator

Answer: Option B

Explanation:

No answer description available for this question.

Ravi(Bit Mesra Cse_2K7) said: (Oct 21, 2010)  
Any bit AND(&) with 0 will give a zero .i.e. will turn that particular bit OFF.

Suhas . U said: (Nov 26, 2010)  
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

Arjun Prasad said: (Feb 28, 2011)  
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: (Feb 28, 2011)  
~(1<<"bit position") is the operand

Sohan Lal Mits Gwalior said: (Mar 1, 2011)  
Bitwise 'AND' of any bit with zero bit is always zero (off).

Kumar said: (Apr 23, 2011)  
Could anyone give an explanation in brief with example the above examples could not be understood

Sundar said: (Apr 24, 2011)  
@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!

Ramya said: (Jul 8, 2011)  
Thanks sundar.

Anuradha Sharma said: (Sep 2, 2011)  
If we take any bit AND (&) with 0 will give a zero. (i.e) Will turn that particular bit OFF.

Saurav said: (Sep 5, 2011)  
But! why will && not work?

Heena said: (Sep 16, 2011)  
Because && is a logical operator not a bitwise (bitwise is a operator that takes single bit at a time).

Bari said: (Sep 22, 2011)  
Please explain about bitwise operator?

Bari said: (Sep 22, 2011)  
How to work bitwise ?

Vinod Basi said: (Nov 4, 2011)  
Sorry, I know this is an outdated thread... But just saw Mr.Gaurav's doubt to be one which I had a days back.
Here is a simple explanation, correct me if am wrong.

@Saurav

Its like && and || are logical operators, evaluting true statements.
Eg:
Sachin && Sehwag opens batting, It returns true(1) only if both the guys are present to open the game. Else False(0)

Sachin || Sehwag, return true(1), if either Sachin or Sehwag accompanied by someone else opens the game. Else false(0)

& and | are bitwise operators, High and Low
AND, OR gates truth table pretty much explains it.
Eg:
2 & 3 (AND)
010
&
011
------
010

2 | 3 (OR)
010
|
011
------
011

Bitwise operators can be predominantely found in Embedded design, to find the status (ON or OFF) of a bit in a system.


Hope this explains...

Cheers...

Athi said: (Nov 29, 2011)  
I have 1 doubt please tell me how to find the status of a bit ie char s[50];

Then write a c program to find the status of 170th bit.

Satyaprakash said: (Jan 12, 2012)  
Turn off operator in bitwise operators is and because it turns 1&0 and 0&1 into turn off(that is o). |(OR) bitwise turns preceeded operations into turn on(that is 1).

Hence AND IS turn off operator.

Naveen Kumar Ramisetty said: (Feb 2, 2012)  
Thanks ramya.

Deepanwita said: (Jul 26, 2012)  
Bitwise operator is more easily been applied than the logical operator.

Mouni said: (Sep 3, 2012)  
Please explain how to convert hexa decimal into binary.

Saiprabha said: (Sep 7, 2012)  
If you want to off the bit means u want to make it ZERO it is possible in only & condition because
in & if 1 1 o/p-1
else 0

Dinesh said: (Sep 18, 2012)  
I explain in simple manner

bit wise & truth table
=========================
a b z
=========================
0 0 0
1 0 0
1 1 1
0 1 0
==========================
only one condition satisfyied foe on bit excpt bit value is off position

Prema Latha.S said: (Sep 28, 2012)  
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.

Umesh said: (Jun 25, 2013)  
How to convert hexadecimal into binary?

Answer:

1) Take the hexa decimal no. and calculate it decimal value first.

2) Convert decimal number into binary having 4 digit.

3) Repeat 1 and 2 until number is not end.

e.g.

A2F for A decimal is 10.

and 10 = 1010 in binary in the same way.
2 = 2 in decimal and in binary 0010.
F = 15 in decimal and in binary 1111.

So Hexa(A2F) = Binary (1010 0010 1111).

Sudheer said: (Aug 23, 2013)  
Hai friends I am new to C language programming I am unable to understand the logics in this languages in bitwise operation can any explain this in simple way please?

Kannan P said: (Sep 3, 2013)  
Given 4 option A and C not a bit-wise operator. B and D is a bit-wise operator. If you use D it will totally changed the values in high to low and low to high. But use & operator mask bit. We can change where ever you want.

Ajarmani said: (Sep 6, 2013)  
@Sudheer.

The basics of bitwise operation is:
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0

1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0

Using this we will solve an example.

5 & 3
1.Convert to binary
101 & 011.

2. Now apply bitwise operation:
1 0 1 = 5
& & &
0 1 1 = 3
_____
0 0 1 = 1

Hence 5 & 3 = 1.

Ranjana said: (Sep 19, 2013)  
Any bit AND(&) with 0 will give a zero .i.e. will turn that particular bit OFF.

Mallikarjun said: (Aug 25, 2014)  
Bitwise & is used for masking bits i.e 1 can be done 0.

Sudha said: (Nov 6, 2014)  
And operator follow a multiple mechanism. So Zero the position will be off, the OR operator addition mechanism.

Apshana said: (Jul 15, 2015)  
Why && will not be answer? And tell me what is the difference between & and &&?

Nkandu said: (Sep 26, 2015)  
I am new to c programming I don't understand it can someone please help me.

Simanta said: (Nov 7, 2015)  
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.

Deepak_Bboy said: (Dec 25, 2015)  
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.

Pritam said: (Jan 12, 2016)  
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.

Emanuel said: (Sep 29, 2016)  
I think ^ operator.

Shanme said: (Oct 18, 2016)  
Simply we can say, on the given option everything is logical operator, the only bitwise operator is &,

So answer &.

Sateesh said: (Mar 6, 2017)  
I think whether in AND case both are true then only particle turned on. So I think OR case is the correct answer.

Anyone explain it.

Gafoor said: (Nov 23, 2017)  
how to work & operator in the program?

Can anyone explain with an example, please?

Anusha said: (Dec 13, 2017)  
! 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?

Ruhi said: (Oct 26, 2018)  
Thanks all for explaining this.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.