3 ms·
> "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
by 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).