Display the bits within a byte. : binary bit « Data Types « C++ Tutorial






#include <iostream> 
using namespace std; 
 
void show_binary(unsigned int u);
 
int main() 
{ 
  show_binary(89);
 
  return 0; 
}

void show_binary(unsigned int u) 
{ 
  int t; 
 
  for(t=128; t>0; t = t/2) 
    if(u & t) cout << "1 "; 
    else cout << "0 "; 
 
  cout << "\n"; 
}
0 1 0 1 1 0 0 1








2.13.binary bit
2.13.1.Printing an unsigned integer in bits
2.13.2.Display the bits within a byte.
2.13.3.Bitwise operators: Complement, AND, OR
2.13.4.Bitwise operator: ^
2.13.5.Use masks to select or set a particular flag bit