Using a mask to pack values into a variable - C++ Operator

C++ examples for Operator:Bit Operator

Description

Using a mask to pack values into a variable

Demo Code

#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    const int maskBits{ 16 };
    int leftShifted{ 0x00001010 << maskBits };
    cout << showbase << hex;
    cout << "Left shifted: " << leftShifted << endl;

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

    return 0;/*from   w ww.  ja  v a2s.  c  o  m*/
}

Result


Related Tutorials