Output integer and its 1's complement in decimal and hexadecimal. - C++ Data Type

C++ examples for Data Type:int

Description

Output integer and its 1's complement in decimal and hexadecimal.

Demo Code

#include <iostream>
#include <iomanip>

int main(){/* w  w w.  j a  v  a2 s.  c  o m*/
  unsigned int value {};
  std::cout << "Enter a positive integer: ";
  std::cin >> value;
  unsigned int inverted_value {~value};

  unsigned int hex_digits {2 * sizeof(unsigned int)};       // Hex digits in value
  unsigned int width {hex_digits + 2};                      // Add 2 for 0x prefix
  unsigned int column_width {2 * width};                      // Output column width

  // Output column headings
  std::cout << std::right << std::setw(column_width) << "value"
    << std::setw(column_width) << "~value"
    << std::setw(column_width) << "~value+1" << std::endl;

  // Output hexadecimal values
  std::cout << std::hex << std::showbase << std::internal << std::setfill('0')
    << "          " << std::setw(width) << value
    << "          " << std::setw(width) << inverted_value
    << "          " << std::setw(width) << inverted_value + 1 << std::endl;

  // Output decimal values
  std::cout << std::dec << std::setfill(' ')
    << std::setw(column_width) << value
    << std::setw(column_width) << inverted_value
    << std::setw(column_width) << inverted_value + 1 << std::endl;
}

Result


Related Tutorials