5 ms·
My experience as an interviewer showed that in addition to the things you listed, people often fail to grasp binary representation of numbers in a computer at a
by eliben 15y ago
My experience as an interviewer showed that in addition to the things you listed, people often fail to grasp binary representation of numbers in a computer at all.
This most often comes up when people are asked to do some binary manipulation of numbers, i.e.:
unsigned u = 19;
unsigned v = u >> 1;
"v" is now 9, and to really understand it one must grasp how numbers are represented in binary under the hood.
People also fail to understand strings:
char* s = "string literal";
To some, it's absolutely opaque that the first byte "s" points to contains 0x73, and that represents "s" in ASCII.
- irahul 15y ago> "v" is now 9, and to really understand it one must grasp how numbers are represented in binary under the hood. Correct me if I am wrong, but I don't think C guarantees anything about the binary representation. Depending on the architecture, `v` can have different value.
- bad_wolf 15y agoIf 19 is represented differently in binary on your platform than (leading zeros)10011, you are already quite screwed. You might be thinking of character representation for the later example.
- irahul 15y agoNo, I was thinking binary 19. Does C say the implementation is going to be 1s complements, 2s complements or whatever? Is there anything that says, for example, it can't be BCD?
- justincormack 15y agoYes even though this is exactly why people want to use C now, to get exact binary behaviour.
- spc476 15y agoC allows differences in how negative integers are represented (at least, C89 did---I'm not sure about C99 as I don't have the standard in front of me). There are three different ways to represent negative numbers in binary, sign magnitude, one's complement, and two's complement (these days, most computers are two's complement). The upshot is that you might end up with two type of zeros (sign magnitude, one's complement) or an unequal range of negative numbers (two's complement has one additional negative number) and taking the absolute value of an integer may not be possible for some values (given a 16-bit integer on a two's complement system, you cannot get the absolute value for -32,768, which is a valid integer on such a system).
- tzs 15y agoDoes C require that you are running on a binary computer? The C99 standard defines the >> operator in terms of division by powers of 2, so one can determine the result of 19 >> 1 without needing to know anything about how 19 is actually represented by the machine.