How to print a number as binary using cout in C++

1 Answer

0 votes
#include <iostream>
#include <bitset>

int main() {
    unsigned int n = 86; 

    std::bitset<8> binary(n);
    std::cout << binary << '\n'; 
    
    std::cout << std::bitset<8>(n) << "\n";
    
    std::cout << std::format("{:b}", n); // c++ 23
}



/*
run:

01010110
01010110
1010110

*/

 



answered Jul 30, 2025 by avibootz

Related questions

...