C++ examples for Data Type:Binary
Outputting the binary code for a letter.
#include <iostream> #include <cctype> using std::cin;/*from w ww. j a v a 2s. c o m*/ using std::cout; using std::endl; int main() { char ch = 0; cout << "Enter a letter: "; cin >> ch; if(!std::isalpha(ch)) { cout << "That's not a letter!" << endl; return 1; } // Determine upper or lower case. cout << "\'" << ch << "\' is " << (std::islower(ch) ? "lowercase.":"uppercase.") << endl; // Determine whether it is a vowel or a consonant. cout << "\'" << ch << "\' is a "; switch(std::tolower(ch)) { case 'a': case 'e': case 'i': case 'o': case 'u': cout << "vowel."; break; default: cout << "consonant."; break; } // Output the character code as binary cout << endl << "The binary code for \'" << ch << "\' is " << ((ch & 0x80) ? 1 : 0) << ((ch & 0x40) ? 1 : 0) << ((ch & 0x20) ? 1 : 0) << ((ch & 0x10) ? 1 : 0) << ((ch & 0x08) ? 1 : 0) << ((ch & 0x04) ? 1 : 0) << ((ch & 0x02) ? 1 : 0) << ((ch & 0x01) ? 1 : 0) << endl; return 0; }