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 1 of 5.

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.
(1)

Vinod Basi said:   1 decade ago
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...

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!

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.
(1)

Umesh said:   1 decade ago
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).

Ajarmani said:   1 decade ago
@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.

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).
(7)

Dinesh said:   1 decade ago
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

Kannan P said:   1 decade ago
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.

Satyaprakash said:   1 decade ago
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.


Post your comments here:

Your comments will be displayed after verification.