#include #include #include using namespace std; int main() { //Each hexadecimal digit is an abbreviation for a series of four bits. //There are 16 possible hexadecimal digits. const string a[] { "0", //0000 "1", //0001 "2", //0010 "3", //0001 "4", //0100 "5", //0101 "6", //0110 "7", //0111 "8", //1000 "9", //1001 "A", //1010 "B", //1011 "C", //1100 "D", //1101 "E", //1110 "F" //1111 }; const int nbits {32}; //number of bits in an int on our machine const int n {2025}; //In binary, 00000000000000000000011111101001 const int bitmask {15}; //In binary, 00000000000000000000000000001111 //Output the bits of n as 8 hexadecimal digits. //The & bitmask gives us a result in which only the rightmost 4 bits of //shifted survive. for (int i {nbits - 4}; i >= 0; i -= 4) { //loops 8 times const int shifted {n >> i}; const int theBits {shifted & bitmask}; cout << a[theBits]; //thebits is in the range 0 to 15 inclusive. } cout << "\n"; return EXIT_SUCCESS; }