1

I'm trying to check a particular bit of a long long integer

long long int val=23355665641326;
int bit_no=32;
if( (val & (1<<bit_no)) == 0)
  cout<<bit_no<<"'th bit is not set\n";
else
  cout<<bit_no<<"'th bit is set\n";

the binary equivalent of 23355665641326 is -

101010011110111101010001001110110111101101110
            ^

we see, 32'th bit is set. But my code returns not set :(
how can i check the bit?

Kamrul
  • 13
  • 2
  • 2
    Assuming `int` is 32 bits, your code is undefined behaviour because you shifted a 32-bit type by 32+ bits. – chris Apr 20 '15 at 16:41
  • possible duplicate of [How do you set, clear and toggle a single bit in C/C++?](http://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c-c) – NaCl Apr 20 '15 at 16:44
  • 1
    The digit literals like 1, 0 are integers in C/C++. So the expression 1<<32 will never be bigger than an int. I'm not sure if ((long long)1)<<32 would work. I wouldn't even try. I'd always prefer using std::bitset for bit manipulation in C++. – Valentin H Apr 20 '15 at 16:55

3 Answers3

6

Your life would be easy if you use std::bitset instead:

constexpr auto N = CHAR_BIT * sizeof(long long int);

std::bitset<N> val(23355665641326LL);

Now you can test a particular bit as:

if ( val.test(i) ) {
      //bit at index i is set!
}

Or you can use the faster — but unsafe — version, as:

if ( val[i] ) {
      //bit at index i is set!
}

test() performs bound check and throws std::out_of_range if the index is invalid, whereas operator[] does not, in which case invalid index leads to undefined behavior.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 4
    The only proper way to do it in C++ is using std::bitset. – Valentin H Apr 20 '15 at 16:56
  • 1
    @ValentinHeinitz bit masking is no less proper C++. Not everything has to be done with classes – Creris Apr 20 '15 at 17:03
  • 1
    @Creris: classes are not important. Easy APIs are, especially if they dont cost you much, use them as much as possible. I've tested `std::bitset` with handwritten bit-masking, I didn't see any significant difference at all. – Nawaz Apr 20 '15 at 17:05
  • 1
    @Creris: there is no point of using std::string or std::cout but not to using other containers or streams. The only reason, the most people are still inventing their double-linked lists and do C-like bit manipulations is, they are not aware of better techniques. I've really loved bitset as I've written a debugging [tool](https://github.com/vheinitz/jeeamtee) for FPGA communication via shared mem. Bitset allows to use arbitrary bitlength without thinking about platform, compiler, etc. It is explicit and easy to use. – Valentin H Apr 20 '15 at 17:22
  • Works like a charm! – daparic Dec 08 '20 at 19:30
4

You can use 1LL<<bit_no to check the bit status of a long long int.

As you are dealing with long long int you need to use long long type 1
Because if you use normal 1(int), you can check upto 31 bit
So just change the checking portion like this -

if( (val & (1LL<<bit_no) ) == 0)
            ^^^
Ali Akber
  • 3,670
  • 3
  • 26
  • 40
1

To overcome with the need of adding the suffix to the check simply do something like this:

if(!!((val >> bit_no) & 1))
    std::cout << "not set";
else
    std::cout << "is set";
NaCl
  • 2,683
  • 2
  • 23
  • 37