Skip to content

Instantly share code, notes, and snippets.

@sajith
Created October 7, 2019 02:01
Show Gist options
  • Save sajith/7a2c551015e302e127690f378398ea74 to your computer and use it in GitHub Desktop.
Save sajith/7a2c551015e302e127690f378398ea74 to your computer and use it in GitHub Desktop.
Check if a bit is set
#include <iostream>
bool test_bit_set(unsigned num, unsigned idx)
{
return (num & (1 << idx)) != 0;
}
int main()
{
for (unsigned n = 0; n < 8; n++)
{
for (unsigned i = 0 ; i < 8; i++)
{
std::cout << i << "th bit of " << n << " set : "
<< (test_bit_set(n, i) ? "true" : "false") << "\n";
}
std::cout << "\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment