1
$\begingroup$

Let $n\in\mathbb{Z}$ and $0\leq b\leq63$, $b\in\mathbb{N}$. Find the $b$-th bit for the number n on it's $64$ bit representation with sign.

and $T$ be the number of test cases.

This is my attempt:

#include <iostream>
#define f cin
#define g cout
using namespace std;

int T;
long long n;
int b;

int main()
{
    f >> T;
    for(int i = 1; i <= T; ++i)
    {
        f >> n >> b;
        int ans = 0;
        bool ok = true;
        while(n)
        {
            if(b == ans)
            {
                g << n % 2;
                ok = false;
                break;
            }
            n /= 2;
            ++ans;
        }
        if(ok) g << 0;
    }
    return 0;
}

but it does not work on all test cases... also is there another way to do this? or is there another way to store the bits? is there some special libraries? can you do this more efficiently with other tools? can you give me some information to read about bitmasks? and where and when you should use them and how are they usefull?

$\endgroup$
  • $\begingroup$ Sorry but programming questions are off-topic on this site. Also, bit manipulation is a pretty standard thing to do in programming, so it would be surpising if there aren't a ton of resources already on how to do it. $\endgroup$ – David Richerby Nov 11 '18 at 18:20
  • $\begingroup$ Which community should I go to? @DavidRicherby $\endgroup$ – C. Cristi Nov 11 '18 at 18:22
  • $\begingroup$ And what's this community based on? $\endgroup$ – C. Cristi Nov 11 '18 at 18:22
  • $\begingroup$ "My code doesn't work -- please help me fix it!" isn't really on-topic anywhere on Stack Exchange. Programming questions live on Stack Overflow but you'll need to give them more information about what your code is supposed to do and what it actually does if you want ot post there. Each site's help centre has a page about what questions are on-topic: here are ours and Stack Overflow's. $\endgroup$ – David Richerby Nov 11 '18 at 18:36