Using unsigned literals - C++ Data Type

C++ examples for Data Type:int

Introduction

Adding u to the end of a numeric literal marks literal as an unsigned value.

Demo Code

#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
  const unsigned int maskBits{ 16 };
  unsigned int leftShifted{ 0x00008080u << maskBits };
  cout << showbase << hex;//from   w w w  . ja v a  2 s  .com
  cout << "Left shifted: " << leftShifted << endl;

  int lowerMask{ 0x0000FFFF };
  leftShifted |= (0x11110110 & lowerMask);
  cout << "Packed left shifted: " << leftShifted << endl;

  unsigned int lowerValue{ (leftShifted & lowerMask) };
  cout << "Lower value unmasked: " << lowerValue << endl;

  int upperValue{ (leftShifted >> maskBits) };
  cout << "Upper value unmasked: " << upperValue << endl;

  return 0;
}

Result


Related Tutorials