Does 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.
> "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.
C 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).
If 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.
No, 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?
Re:Learn C The Hard Way
by justincormack on Sunday July 03, 2011 @05:33AM
(#2723413)
Yes even though this is exactly why people want to use C now, to get exact binary behaviour.
This most often comes up when people are asked to do some binary manipulation of numbers, i.e.:
"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:
To some, it's absolutely opaque that the first byte "s" points to contains 0x73, and that represents "s" in ASCII.