Saturday, October 4, 2008

Guess what's printed out?

I'm not supposed to be posting anything, but I just thought of this simple puzzler (I'm sure I'm not the first one who thought of it, but it's interesting anyway).

#include <iostream>

int main() {
short a;
int b;

a = b = 65535;
std::cout << a << std::endl;
}

I thought of this problem when watching Lecture 2 webcast of Stanford's CS107. I was bored so I coded it right away and guessed the answer (correctly, fortunately; my 2's complement is not as bad as I thought it was). Watch the lecture here.

EDIT: It's too easy? I hear you. (: So here is a second, harder version of it:

#include <iostream>

int main() {
int a = 65535;
short b = *((short*)&a);
short c = *((short*)&a + 1);

std::cout << b << " " << c << std::endl;
}

Hint: If you thought you've solved it and are very confident with your answer, here's a hint; yes, this hint is only for people who has already solved it and confident with your answer. The hint is "it depends".

While these quizzes look like it's created purely to torture people (yes, that thought crossed my mind, and hear hear, I did have a good share of laugh when some suffering friends came crawling begging to be released from this torment...). No, just kidding (the laugh part), we mostly ended up having great discussions on what's happening, and you get to learn a lot from those discussions.

- Chris

15 comments:

juliana said...

To the 2nd quiz, i'm guessing -1 and -1 for 4-byte int and 2-byte short

chris said...

whaa.. not bad. that's fast. but incorrect. d:

let's make an assumption that int is 4-byte to reduce the uncertainty (some older machines have int that are sized at 2 bytes).

P.S. I made a slight typo up there, it's supposed to be printing out b and c (not a and b).

juliana said...

oh yeah, i was referring to b and c too. anyway juat ran it and got the "correct" answer. hrm, i thought (short*)&a + 1 does pointer arithmetic

chris said...

hoho. you cheated! d: though, the amazing thing about this quiz is that when you compiled it using GCC and run it, the results printed just confuses you further.

and yes. it does pointer arithmetic. change a to 1 and see what happens. i.e. int a = 1;

juliana said...

heh heh, well to avoid early spoilers (to quiz nuts out there), i know why it prints the way it does when a = 1 or when a = 131072.
but still bewildered why a = 65535 gives the answer it does. don't tell, i'll be back later

chris said...

What about the "it depends" part? That's supposed to be the fun part.. d:

juliana said...
This comment has been removed by the author.
chris said...

when you tell me why "it depends", i might think of another one.. d:

by "it depends" i mean the answer is not always what you think it is.

chris said...

oh, btw, you don't need to explain the entire thing, just mention the key words. (:

juliana said...

EDIT:
(editing potential spoiler comment, since chris revels in mental torture =p)

OWH pft pft!
thought int a was much larger than it is. no wonder. since you eat sleep live computers, maybe you'd have time to think of a level 3 quiz =)

chris said...

haha.. umm.. you still didn't answer the "it depends" part. meanwhile, i have a nice question that i always asked my (ex-)students. when i feel like typing it, i'll type it out. (:

juliana said...

"by "it depends" i mean the answer is not always what you think it is."

i think so too, but can i figure that out with one machine?

chris said...

hmm.. good enough.. (:

you're the first the get the correct answer, not too shabby.. d: though your opponents were both second years.

juliana said...

with concrete fundamental knowledge first-years should be able to answer them too, by the 2nd semester at most

chris said...

Ugh.. Hmm.. That would make them fire up enough d:

Anyway, as you wish, Ojou-sama. I've posted a longer quiz for you. It's easy, but fun.