Jump to content
Can't remember your login details? Read more... ×
robzy

Write 63 in binary

Recommended Posts

One of my coworkers likes to ask people to write 63 in binary when he interviews them.

 

Some of you might be thinking “that’s stupidly easy”, and many of you may be thinking “I have no idea and I hope I’m never asked that!”

 

There are many amusing answers to this request. Chief among them is to claim that they (in a computer or IT related field) have no need to know how to write a number in binary. That is the wrong answer. And shame on you for even considering such a thing — change professions immediately! If this question doesn’t appeal to you, then everything in Computer Science and Mathematics will seem like confusing magic and you’ll likely embarrass yourself on a daily basis. This is true for Project Managers, which is likely why so many PMs embarrass themselves all too often.

 

The correct answer, of course, is not to blurt out the binary string without even thinking, that would appear arrogant at best and cheating at worst. The correct answer is to either write or visibly start plotting out successive powers of 2, and upon reaching 64 (1000000) quickly slap your head at the realization that 63 is, in binary, 1 minus 1000000 therefor 0111111. You get partial credit for knowing, but demonstrating that you realized it was a silly question will get you full credit.

 

Now here is where the interview can get interesting. Why would a number like this be useful? What would happen if I bit-shift left or right? Suddenly a basic understanding of Boolean logic and Computer Science are uncovered! And at that point it becomes clear who is storing IP addresses as varchars and who is storing them as integers.

http://verbose.net/blog/archives/11

 

Okay, so I would have been able to write 63 in binary fairly easily - I would have known I should start writing 1s rather than 0s, and just stop before I hit 64.

 

What I'm not at all sure about is "Why would a number like this be useful? What would happen if I bit-shift left or right?", anyone care to discuss it? :P

 

Rob.

Share this post


Link to post
Share on other sites

63 is useful because you can use it to force a number to be divisible by 64 by performing a binary AND with the inverse of 63. Why I will leave as an exercise for the reader. Of course you might ask why you want to do this, and there are many reasons, one of which is producing a 64 byte aligned pointer.

 

If you bit shift left or right, then you would multiply or divide it by 2^n, where n is the size of the shift. Provided you don't run out of bits that is. It is a the fastest way of doing division/multiplication by 2.

Share this post


Link to post
Share on other sites

Ahh, interesting freespace :)

 

Two questions though - Why inverse do you mean "^-1"? And how exactly does one "force" a number to be divisible by 64? :P

Share this post


Link to post
Share on other sites

Ahh, interesting freespace :)

 

Two questions though - Why inverse do you mean "^-1"? And how exactly does one "force" a number to be divisible by 64? :P

By inverse, I mean the computer-version of the term where you flip the bits. As for your second question, consider what all numbers divisible by 64 have in common, their bit patterns, and how that relates to the bit pattern of 63.

 

It is an exercise for the reader remember? :P

Share this post


Link to post
Share on other sites

Heh.. I actually interpreted that as write "63" in binary... which is 00110110 00110011.

 

The main reason why I'd use a varchar type for IP addresses, is because it makes it a lot easier to debug and saves time actually parsing the IP address.

 

Plus, if you say allocate a 32-bit type (long integer) you miss out on IPv6 compatibility, which varchar will handle quite easily. I'm not sure there is a long long long int data type (128-bits), so you'd be splitting the address over 4 long ints, or two long long ints. Very messy.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×