gcc/g++ 4.9.2; Unexpected output?

Learning C++ from a book.

I set a value for an unsigned long int called red:

unsigned long int red = 0xFF0000UL;

The hexadecimal output of red is: 0x00FF0000

Which is what I expected.

However, the output for ~red is:
FFFFFFFFFF00FFFF

Which is definitely not what I expected.

I expected FF00FFFF

2015-03-06T13:41:42Z

The source code that produced the unexpected output is:

// PROGRAM 03-04: USING THE BITWISE OPERATORS

#include <iostream>
#include <iomanip>

using std::hex;
using std::cout;
using std::endl;
using std::setw;
using std::setfill;

int main() {

unsigned long red = 0xFF0000UL; // Variable representing the color red
unsigned long white = 0xFFFFFFUL; // Variable representing the color white

cout << hex; // Set the output format to hexadecimal
cout << setfill

2015-03-06T13:43:38Z

I copy/pasted and the copy/paste was incomplete.

PLEASE DISREGARD THIS QUESTION. I'LL DELETE IT WHEN I FIGURE OUT HOW.

_Object2015-03-06T13:39:24Z

Favorite Answer

Does
10 equal 000010?

Since the standard does not mandate the widths of types in general, the four leading bytes set high in the compliment are correct. On your system, `sizeof red' is 8.

deanyourfriendinky2015-03-06T13:54:34Z

Okay, so I can't edit what I've already written. That sucks. Anyway, the point of the program was to demonstrate "using the bitwise operators". The purpose was to flip the bits in variables that would be representing the colors red and white (because the lesson in the book was about animating with two colors, a background color and a color of a simple shape). Anyway, here's the source code, correctly copy/pasted:

// PROGRAM 03-04: USING THE BITWISE OPERATORS

#include <iostream>
#include <iomanip>

using std::hex;
using std::cout;
using std::endl;
using std::setw;
using std::setfill;

int main() {

unsigned long red = 0xFF0000UL;
unsigned long white = 0xFFFFFFUL;

cout << hex;
cout << setfill('0');

cout << "\n\tTry out the bitwise operators AND (&) and NOT (~).";

cout << "\n\tInitial value:\t\tred\t\t=\t" << setw(8) << red;
cout << "\n\tComplement:\t\t~red\t\t=\t" << setw(8) << ~red;

cout << endl << endl;

return 0;
}

Of course, there is more to the program, but I wanted to get past the first unexpected output, so I was just using the first few lines to try to figure out why the output of ~red was 64 bits long (16 hexadecimal digits), when all it was supposed to be was 32 bits long (8 hexadecimal digits).