C++ Online Compiler
Example: Decimal to Binary Conversion (std::bitset) in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Decimal to Binary Conversion (std::bitset) #include <iostream> #include <bitset> // Required for std::bitset int main() { // Step 1: Declare variable int decimalNum; // Step 2: Prompt user for input std::cout << "Enter a non-negative decimal number: "; std::cin >> decimalNum; // Step 3: Choose a fixed width for the binary representation // Common widths include 8, 16, 32, 64 bits. // Ensure the number fits within the chosen width. if (decimalNum < 0) { std::cout << "Please enter a non-negative number." << std::endl; return 1; } std::cout << "Binary equivalent (8-bit): " << std::bitset<8>(decimalNum) << std::endl; std::cout << "Binary equivalent (16-bit): " << std::bitset<16>(decimalNum) << std::endl; std::cout << "Binary equivalent (32-bit): " << std::bitset<32>(decimalNum) << std::endl; return 0; }
Output
Clear
ADVERTISEMENTS