-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_print.cpp
More file actions
32 lines (26 loc) · 1.01 KB
/
binary_print.cpp
File metadata and controls
32 lines (26 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <print> //C++23
#include <format> //C++20
#include <bitset>
int main()
{
int x{20};
// Convert the objects' output format
std::cout << "Hexadecimal:" << std::hex << x << '\n';
std::cout << "Octal:" << std::oct << x << '\n';
// bitset
// Set binary object and output them directly
std::bitset<8> b1{0b1000'1001}; // which can be initialized by a value in other format - binary with prefix
std::bitset<8> b2{0xC3};
std::cout << "Bitset<8>:" << "b1: " << b1 << " " << "b2: " << b2 << '\n';
// C++20 more flexiable
// Output binary format
std::cout << __cplusplus << '\n';
std::cout << std::format("{:b}\n", 0b1010);
std::cout << std::format("{:#b}\n", 0b1010); // keep the 0b-prefixed
// Output Hexadecimal Octal Decimal
std::cout << std::format("Hexadecimal:{:#x}, Decimal:{:#d}, Octal:{:#o}", 0b1010, 0b1010, 0b1010);
// C++23
// std::println("{:b} {:#b}", 0b1010, 0b1010);
return 0;
}