IQOD #14

Posts 1–9 of 9 · Page 1 of 1
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?
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...
Darn, fail double post
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? -->
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
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... >_<
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
BTW: BA this isn't funny at all unless you find the idea of inflicting pain on others funny o_O
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 =)
Posts 1–9 of 9 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us