answersLogoWhite

0


Best Answer

To determine if a specific bit within a bitset is on or off, use the bitwise AND operator. Note that a bitwise AND is not the same as a logical AND.

Logical AND (or &&) is used in Boolean logic:

x && y is true when x and y are both true, otherwise it is false.

Bitwise AND (or &) compares each corresponding bit, returning a value that indicates which bits are set in both values. This is best seen when the two values are placed one above the other so we can compare the corresponding bits:

10101100 &

10110101 =

10100100

From this we can see that the 3rd, 6th and 8th bits are set in both values.

Knowing this, we can determine the state of any individual bit in any value by simply bitwise ANDing the value with the specific bit we are looking for. If the result is zero, then the bit is not set but if the result is nonzero, then it is set.

As an example, let us suppose we want to test if bit 4 is set. Note that bit 4 is not the same as saying the 4th bit. Bit 4 is actually the 5th bit because bit 0 is always the least significant bit in binary. The reason is that bit 0 represents 2 raised to the 0th power which is 1 in decimal or 00000001 in binary. Bit 4 therefore means 2 raised to the 4th power which is 16 decimal or 00010000 in binary.

In decimal notation, we can determine if bit 4 is set in variable named x like so:

x = 181;

if ((x & 16) == 0)

{

// bit 4 is not set

}

else

{

// bit 4 is set

}

Normally it is easier to use hexadecimal notation as it makes it more obvious we are performing a bitwise operation upon binary values rather than decimal values (which is notationally confusing). Given that 16 is 0x10 in hexadecimal, we then get:

x = B5;

if ((x & 0x10) == 0)

{

// bit 4 is not set

}

else

{

// bit 4 is set

}

Note that ((x & 0x10) == 0) is the same as saying (!(x & 0x10)) while ((x & 0x10) != 0) is the same as saying (x & 0x10). The latter is notationally simpler, thus it often makes more sense to reverse the logic:

x = 0xB5;

if (x & 0x10) // read as: if (x & 0x10) is nonzero

{

// bit 4 is set

}

else

{

// bit 4 is not set

}

If we suppose that x is 0xB5 (181 decimal), then we can see more clearly how this works by examining the binary notation:

0xB5 : 10110101 &

0x10 : 00010000 =

0x10 : 00010000 (non-zero, so bit 4 is set)

If we now suppose that x is 0xA5 (165 decimal), then we get:

0xA5 : 10100101 &

0x10 : 00010000 =

0x00 : 00000000 (zero, so bit 4 is not set)

Note that when we're only checking for one bit and that bit is set, the return value is always the same value as the bit we are actually checking for. But when we check for more than one bit (as per the original example at the top of this answer), the result is either zero (none of the bits are set), or some combination of the bits we were looking for (one or more of the bits are set). Therefore it's easier to say the result is either zero or non-zero regardless of how many bits we're checking for.

Another notation we often encounter is the following:

x = 0xB5;

if (x & (1<<4))

{

// bit 4 is set

}

else

{

// bit 4 is not set

}

It looks more complex at first, but it is actually more readable than the hexadecimal notation. 1<<4 is a bit-shift left operation that shifts a 1 four bits to the left (>> would shift to the right). 1 in binary is 00000001, thus if we shift left 4 times we get 00010000. Thus 1<<4 is effectively the same as saying bit 4. By the same token, 1<<7 would mean bit 7 while 1<<2 would mean bit 2. Expressing ideas directly in code like this means we don't need to comment our code so much; it becomes self-documenting.

Note that bit-shifting like this only works when we're only interested in a single bit. However, knowing that we can shift bits left or right means we can more easily determine the status of every bit using a simple for loop:

x = 0xB5;

for (i=0; i<8; ++i) // assumes x is an 8-bit value

{

if (x & (1<

{

// bit i is set

}

else

{

// bit i is not set

}

}

If we want to output the individual bits to a character stream such as standard output, we need to work in reverse, starting from bit 7:

x = 0xB5;

b = 1<<7; // bit 7

while (b)

{

if (x & b)

{

std::cout << '1'; // note: << means 'insert' in this context

}

else

{

std::cout << '0';

}

b = b>>1; // bit-shift right

}

std::cout << std::endl;

Note that languages like C++ allow the same operator to perform different operations depending on the context. Hence << can be used to bit-shift a value or to insert a character into a character stream. Re-using symbols according to their context like this helps keep the number of symbols to a minimum.

C++ also allows us to write the same code in a more concise form:

x = 0xB5;

b = 1<<7;

while (b)

{

std::cout << (x & b ? '1' : '0');

b = b>>1;

}

std::cout << std::endl;

Of course, there is an even easier way to print an 8-bit binary value in C++:

x = 0xB5;

std::bitset<8> b (x);

std::cout << b << std::endl;


User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you check a bit is on or off?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What to do if your computer says check LBA translation mode?

Go into your BIOS and check for where it says Bit shift and check that box, you obviously cant use LBA or leave it as OFF.


Can a baby rabbit survive after having a leg bit off?

Yes, if you look after it well, and make sure you have regular check ups.


When was A Bit Off the Map created?

A Bit Off the Map was created in 1957.


Your heater core is leaking a little bit what do you need to check?

How much money you have. There is no such thing as a "little bit" as eventually if it's not taken care of, it will burst. Won't save you any money to put it off.


How to check which compiler i am using 16 bit or 32 bit ...?

If you are using TC.EXE (or TCC.EXE) then it is 16-bit.


Is ajaz quershi a female?

bit off both if yu no what i mean :o bit off both if yu no what i mean :o bit off both if yu no what i mean :o


Did holyfield sew his ear back on after Tyson bit it off?

no holyfielsawhis earback on Tyson bit it off


What tiny unit of information storage consisting of an onoff signal?

Bit.


Where is the area to check transmission fluid in 1993 ford Taurus?

Dipstick is behind the engine near the firewall. It is just a bit off of center toward the driver's side.


Is used for error detection where the bit is turned on or off depending on whether the sum of the other bits in the byte is even or odd?

it is called even / odd parity check


How do you top off ATF on a 2005 Tacoma?

Check the fluid level at the dipstick according to the instructions written in you owners manual. You my have to check it with the engine running and you may not. If it is low add fluid slowly a little bit at a time. Do not overfill.


What animal's did Ozzy bite off on stage?

He bit heads off of Fake bats, but once he bit the head off of a real bat.