Thread: IQOD #14

Results 1 to 9 of 9
  1. #1
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical

    IQOD #14

    Ok One more funny one

    Consider the following code:
    Code:
    union Data
    {
    public:
    	unsigned short a;
    	unsigned int b;
    	unsigned char c;
    	unsigned long long d;
    };
    
    void main ()
    {
    	Data data;
    	data.a = 32123;
    	data.b = 53129876;
    	data.c = 128;
    	data.d = 908292342362633;
    
    	cout << "a=" << data.a << endl;
    	cout << "b=" << data.b << endl;
    	cout << "c=" << (unsigned int)data.c << endl;
    	cout << "d=" << data.d << endl;
    	
    	system("pause");
    	
    }
    What is the output it generates?

  2. #2
    zeco's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    Canada
    Posts
    683
    Reputation
    12
    Thanks
    78
    My Mood
    Cynical
    oh hey, Don't unions only hold one member at a time? Thus only data.d would output, and the others would give an error or throw random numbers at you.
    Je pense...

  3. #3
    zeco's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    Canada
    Posts
    683
    Reputation
    12
    Thanks
    78
    My Mood
    Cynical
    Darn, fail double post
    Last edited by zeco; 09-29-2009 at 07:13 PM.

  4. #4
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical
    Zeco: <!-- Not quite, but I can see why you remembered it that way... with unions they live in the same memory space. Does that give any insight? -->

  5. #5
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    i know d would be the value you assigned to it because you assigned it last. but have on idea what the other ones would be
    Ah we-a blaze the fyah, make it bun dem!

  6. #6
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    Wow. this is going to be wierd. All sharing the same memory space huh? BTW I've never seen anyone write declare a long long before :P

    EDIT: I just read what HD wrote so the opposite of this, because the last assignment would overide. And Oh god. It's in binary so You would have convert 908292342362633 into binary then map out each part. Hell I'm not doing this conversion I'll use the internet:
    0000 0011 0011 1010 0001 0110 [0100 1010 0110 1011 [0011 0010 [0000 1001]]]
    110011101000010110 01001010011010110011001000001001

    Ok. a Char is a Byte
    short is 2 Bytes or Word
    int is 32 bits or 4 Bytes or DWord
    long is 64 bits or QWORD
    long long is 256 bits or 4 QWords!

    This number fills atleast 7 Bytes so what's printed is:

    12809
    4877106
    9
    908292342362633


    I think... >_<
    Last edited by why06; 09-30-2009 at 01:14 PM.

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  7. #7
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical
    long long is just making sure it's 64 bit

    and yes both HD & Why are on the right track.. so whip out your windows calculator and get to it

  8. #8
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    BTW: BA this isn't funny at all unless you find the idea of inflicting pain on others funny o_O

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  9. The Following User Says Thank You to why06 For This Useful Post:

    Hell_Demon (09-30-2009)

  10. #9
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    Quote Originally Posted by why06 View Post
    BTW: BA this isn't funny at all unless you find the idea of inflicting pain on others funny o_O
    QFT :P

    Calculator eh? silly hex haxxed my answer!

    908292342362633 is 33A164A6B3209 in hex
    short is 2 bytes, so we take the last 4 numbers of the hex(since 1 byte can be 255 max, which is FF in hex) =)
    3209(hex) = 12809(decimal)
    so:

    a=12809

    b is an int which is 4 bytes, so the last 8 numbers from the hex (4A6B3209)
    4A6B3209(hex) = 1248539145(decimal)

    a=12809
    b=1248539145

    c is a char, which is 1 byte so the last 2 from the hex
    09(hex) = 9(decimal)

    a=12809
    b=1248539145
    c=9

    d stays the same

    a=12809
    b=1248539145
    c=9
    d=908292342362633


    i win =)
    Ah we-a blaze the fyah, make it bun dem!

Similar Threads

  1. IQOD #4
    By B1ackAnge1 in forum C++/C Programming
    Replies: 7
    Last Post: 09-20-2009, 01:37 PM
  2. IQOD #2
    By B1ackAnge1 in forum C++/C Programming
    Replies: 37
    Last Post: 09-19-2009, 04:40 PM
  3. IQOD #3
    By B1ackAnge1 in forum C++/C Programming
    Replies: 23
    Last Post: 09-19-2009, 01:15 PM
  4. Extra Credit IQOD #2A
    By B1ackAnge1 in forum C++/C Programming
    Replies: 18
    Last Post: 09-18-2009, 06:39 PM
  5. Interview Question of the Day (IQOD) #1
    By B1ackAnge1 in forum C++/C Programming
    Replies: 26
    Last Post: 09-18-2009, 04:24 PM

Tags for this Thread