How to print the bits of a character in C++

1 Answer

0 votes
#include <iostream> 
 
using namespace std; 
 
void print_bits(char ch) { 
   for (int i = 31; i >= 0; i--)
        cout << ((ch >> i) & 1);
} 
    
int main() 
{ 
    char ch = 'a';
      
    print_bits(ch);
     
    return 0; 
} 
 
 
/*
run:
 
00000000000000000000000001100001
 
*/

 



answered Mar 6, 2019 by avibootz

Related questions

1 answer 145 views
145 views asked Jul 13, 2022 by avibootz
3 answers 275 views
1 answer 237 views
1 answer 149 views
...