Binary format & literals in C++

cout « std::bitset<32>( 0b1000’0000’0000’0000 ) - Binary solo

C++14 binary literals and digit separator

auto binary=0b10001000 ; //binary digit
auto hex=0x12ab; ///hexadecimal number
auto octal=0127 ; //octal number

auto B16=0b1000000000000000 , //easier to guess the number 

Printing numbers in binary format in C++

In C++20 you’ll be able to use std::format to do this

std::cout << std::format("{:b}", a);

otherwise

The easiest way is probably to create an std::bitset representing the value, then stream that to cout.

#include <bitset>
  
int v = 0x12345678;
std::cout << std::bitset<32>(v);

see also

Written on June 12, 2021, Last update on October 10, 2022
c++ bits literals