Set output format to hexadecimal - C++ Data Type

C++ examples for Data Type:int

Description

Set output format to hexadecimal

Demo Code

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
    // set output format to hexadecimal
    cout.unsetf(cout.dec);// w  ww  .  j  a v a2  s.  c  o m
    cout.setf(cout.hex);

    // initialize two arguments
    int nArg1 = 0x78ABCDEF;
    int nArg2 = 0x12345678;

    cout << " nArg1 = 0x" << nArg1  << endl;
    cout << "~nArg1 = 0x" << ~nArg1 << "\n" << endl;
    cout << " nArg2 = 0x" << nArg2  << endl;
    cout << "~nArg2 = 0x" << ~nArg2 << "\n" << endl;

    cout << "  0x" << nArg1 << "\n"
         << "& 0x" << nArg2 << "\n"
         << "  ----------" << "\n"
         << "  0x" << (nArg1 & nArg2) << "\n"
         << endl;

    cout << "  0x" << nArg1 << "\n"
         << "| 0x" << nArg2 << "\n"
         << "  ----------" << "\n"
         << "  0x" << (nArg1 | nArg2) << "\n"
         << endl;

    cout << "  0x" << nArg1 << "\n"
         << "^ 0x" << nArg2 << "\n"
         << "  ----------" << "\n"
         << "  0x" << (nArg1 ^ nArg2) << "\n"
         << endl;

    return 0;
}

Result


Related Tutorials