Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

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

Update:

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

Update 2:

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

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

2 Answers

Relevance
  • 6 years ago
    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.

  • 6 years ago

    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).

Still have questions? Get your answers by asking now.