-1

I have a question with a problem in c++. I have to create a program where I have to create one variable from Int and to cout<< on the screen "True" if the 3rd bit is 1.

My question is: How can I see what is the 3rd bit of that number; I've tried with bitset, but couldn't solve it. Please help me.

#include<iostream>
#include<bitset>
using namespace std;
int main()
{
int x; cin >> x;

if (x % 3 != 0 && bitset<32>(1)[2])
{
    cout << "TRUE";
}
else
{
    cout << "FALSE";

} This should do it right ?

BerryTan
  • 11
  • 5
  • 1
    you mean the third bit from left or right? – Rratic Nov 04 '21 at 14:10
  • 1
    *"I've tried with , but couldn't solve it"*. Tried with what? Tried with what??? – JohnFilleau Nov 04 '21 at 14:10
  • The task is if that variable can't be divide by 3 and the 3rd bit is 1 then to show True. From left to right, after the decimal vergule. – BerryTan Nov 04 '21 at 14:13
  • @JohnFilleau I tried with bitset, but I don't know how it works – BerryTan Nov 04 '21 at 14:14
  • Assuming you mean first bit being least significant one, then third bit has index 2 and you get it via `number >> 2 & 1`. If first one is most significant one, then you need to shift a bit more, considering the size of the type: `number >> sizeof(number) * CHAR_BIT - 3 & 1`. – Aconcagua Nov 04 '21 at 14:19
  • So, where exactly did you get stuck with the [constructor No 2 here](https://en.cppreference.com/w/cpp/utility/bitset/bitset) and [the bit access operator](https://en.cppreference.com/w/cpp/utility/bitset/operator_at)? Please post your code. – Andrej Podzimek Nov 04 '21 at 14:24
  • just do a bitwise-and (& symbol) with the mask 0x0004 –  Nov 04 '21 at 14:32

2 Answers2

1

Checking if a given bit is set is a classic pattern that you will encounter in a great many codebase. So even if there are cleaner ways to do it in modern C++, it's still worth being able to at least recognise the old school pattern when it pops up:

// You will typically see bit masks predefined in constants or an enum.
enum flags {
  FEATURE_1 = 1 << 0,  // first bit
  FEATURE_2 = 1 << 1,  // second bit
  FEATURE_3 = 1 << 2,  // third bit
  FEATURE_4 = 1 << 3,  // fourth bit
};

if(value & FEATURE_3) {
  // the bit is set
}
else {
  //the bit is not set
}

Explanation:

(1 << bit_index): This creates a mask. I.E. a value with only the bit we care about set. E.G. 1 << 3 is 0b00001000 as a 8 bit integer.

val & mask: This does a binary AND between the value and the mask, which will be 0 if and only if the bit is not set. Since any non-zero value is a true, we just use the result of the & as condition.

You could also shift the value and compare against 1, but doing it the other way around has the advantage that the mask can often be precomputed during compilation, so the check becomes a simple binary AND at runtime.

Nowadays, it is neater to do so with std::bitset:

// Replace 4 with the number of meaningful bits
// N.B. index is still 0-based. 1 means the second bit.
if(std::bitset<4>(value).test(2)) {
  // the bit is set
}
else {
  //the bit is not set
}
0

It depends if "third bit" is the third bit from the left or from the right. In either case I would use bit shifting (>> operator) to move the "third bit" to the first position and than use AND (& operator) with 1. If the bit is set, AND will return 1, if the bit is not set, AND will return 0.

In pseudocode:

x = 0b1011101
          ^ counting this one as the third bit
x = x >> 2 (x is now 0b10111)
is_third_bit_set = x & 1

And will return 1, as:

  10111
& 00001
-------
  00001
whiskeyo
  • 873
  • 1
  • 9
  • 19