// demo_01_ascii_byte_2_binary.cpp /* * @topic T00240 Binary numbers * @brief <br />A program to convert character to its binary representation<br /><br /> */ #include <iostream> // Bit constants to access individual bits, // also known as bit masks: const int BIT_0000_0001 = 1; const int BIT_0000_0010 = 2; const int BIT_0000_0100 = 4; const int BIT_0000_1000 = 8; const int BIT_0001_0000 = 16; const int BIT_0010_0000 = 32; const int BIT_0100_0000 = 64; const int BIT_1000_0000 = 128; int main() { char ch = 'A'; // in ASCII encding, this is binary 01000001, decimal 65 if ( ch & BIT_1000_0000 ) std::cout << '1'; else std::cout << '0'; if ( ch & BIT_0100_0000 ) std::cout << '1'; else std::cout << '0'; if ( ch & BIT_0010_0000 ) std::cout << '1'; else std::cout << '0'; if ( ch & BIT_0001_0000 ) std::cout << '1'; else std::cout << '0'; if ( ch & BIT_0000_1000 ) std::cout << '1'; else std::cout << '0'; if ( ch & BIT_0000_0100 ) std::cout << '1'; else std::cout << '0'; if ( ch & BIT_0000_0010 ) std::cout << '1'; else std::cout << '0'; if ( ch & BIT_0000_0001 ) std::cout << '1'; else std::cout << '0'; std::cout << '\n'; return 0; }