Pretend you're at an interview. The interviewer asks:
What is the decimal value of this binary number?
10110101
What is the first thing you responsed with?
Pretend you're at an interview. The interviewer asks:
What is the decimal value of this binary number?
10110101
What is the first thing you responsed with?
Rightmost bit is least significant, leftmost is most significant.
Unfortunately, that isn't always the case, but I grew up on little endian machines.
EDIT: although if you are trying to point out there are multiple representations, that's a not so great way of asking that. Maybe, how many valid numbers are represented by this bit pattern?
I would probably say something like:
"Errmm... the first half is four..."
(Yeah i know it's five, but i would most likely say 4 dunno why)
This should be community wiki though
Thank you for allowing me to be here.
It was an interesting experience.
Goodbye.
To answer truthfully:
"This is a waste of my time."
I could expound why, but base2/base10 conversion is basic computer math.
When you interview for a position writing for a magazine or newspaper, would you expect to be asked "What is a question mark used for?"
128 plus 32 plus 16 plus 4 plus 1 equals 181.
Actually, I'd probably start with the other end, too. (1 plus 4 plus 16 plus 32 plus 128 equals 181.)
What is the first thing you responsed with?
Your expected answer is wrong: “In what endianness?” – this is not the question any sane person who understands our number system would ask. Why? Because regardless of the base, the endianess is always the same by convention. Always.
To elaborate: there is no reason why different rules should apply for binary than, say, for decimal. Endianess of byte representations only plays a role when used in a context where it obviously matters, because it emulates a particular system or because it encodes information that is written from left to right and should follow this direction (useful when explaining the Bitap algorithm for string search.
Similarly, the question of sign does not occur. A negative number is written with a minus in front of it! Again, the only exception is when dealing with encodings that emulate a specific hardware that is not capable of expressing a sign and needs to encode this in the most significant bit (say).
Seriously though, I see little need to translate binary to decimal in most programming positions
id say 181 because I'm guessing he talking about subnets.
but I have a Cisco background so...
The decimal value of that binary number is 181, or possibly -74 if it is a signed byte. Are you suggesting that other values are possible?
The first thing I'd ask for is a piece of paper. The second thing I'd ask for is a better question.
If it was for a simple scripting job, I'd likely give the simple answer 181. If it was for something more technical that was likely to involve me needing to understand storage of floating point data - for instance communication with hardware devices or teaching computational theory, I'd probably ask a slew of questions similar to:
This would likely be responded to by the interviewer with the following questions:
Or better still, their eyes would glaze over when they failed to understand what I was talking about. At which point I'd give them my original answer of 181 having firmly made the point that if an interviewer doesn't understand the depth of a question they shouldn't be asking it in the first place...
Edit 1: Alternatively I'd be feeling extra cocky and respond with something like
"I can see why you would expect me to say 181, but you'd be wrong."
and then leave them wondering what I meant by that...
Edit 2: Of course, I'd rather have an exam paper with a bunch of these unanswerable questions that most "developers" fail because at least it indicates that the company is trying to hire good developers instead of hiring just anyone off the street - even if they suck at picking good interview questions.
I'd probably wonder why he asked, but the binary number 10110101 equals the decimal number 181 (or the hexadecimal B5 or the octal 265). How binary numbers are used by computer systems does not matter, the question itself is purely about the number.
If you honestly wanted to get the right answer, you'd need to know the encoding of the bits. Bits are just bits until you decide on their meaning.
The encoding is an equation that tells you the value given the digits.. You could assume it's d[0]*2^7+d[1]*2^6+d[2]*2^5+...
(regular unsigned 8 bit int)
Similarly, the digits 1 8 1 may represent apples/oranges/pears with different values each, or the number 181, or the number 1.81, or -81, or +81, or just about anything you could possibly care to think about.
I probably would have answered 181, and just argued with the guy if he seemed to think I was wrong, since his answer can't be right any more than mine can.
Asking about the endianness, or anything, is like saying 7+7=41. Binary is base 2. 10110101 in base 2 is 181. Always. If it was a negative number, it would have a minus sign, if it was fractional, it would have a dot.
There are no standard 8 bit decimal (floating point) formats...
I ask a similar question on my interview "quiz"
Why? Because 75% of the so called "experienced coders" get it wrong.
I also throw in a few Hex/decimal conversions. These are just the first few warm-up questions.
In the OP's question, I would accept any endian, signed, or unsigned as an OK answer.
Then I show code snippets and ask them to add/modify the code to solve a problem, some problems are obviously solved with recursion, others operator overloading. Basically probing the OOP ability of the candidate. I even tell them that they dont really have to get the questions right, they just need to demonstrate they know the concepts. Surprisingly, the vast majority of people who look great on a resume, fail miserably with simple OOP concepts.
I would say: there are 10 types of people, those who understand binary and those who cannot. I'm of the second type.
if it is converted to base10, then wouldn't the answer be simply 181 ?