top button
Flag Notify
Site Registration

Which bit wise operator is suitable for checking whether a particular bit is on or off?

0 votes
606 views
Which bit wise operator is suitable for checking whether a particular bit is on or off?
posted Sep 3, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+2 votes

Use bit wise and (&) operator to check whether a particular bit is on or off.

Example:

ans = paticular bit value & 1

if ans is 1 then the bit is set i.e on
else it is off, i.e not set.

answer Sep 3, 2014 by Arshad Khan
+2 votes

define CHECK_BIT(var,pos) ((var) & (1<<(pos)))

variable=CHECK_BIT(temp, 3)
if the variable is 1. it will on else off.
In c++ you could use std::bitset.

answer Sep 4, 2014 by Double S
Similar Questions
+2 votes

In a 32 bit only one bit is on, with out using loops, goto, or recursion how to find out which position is on?
example : 4 ---> 2nd bit ( 0000 0100)

...