Re: what is 0xFFFFFFF ?
David T. Ashley wrote, On 24/01/07 18:54:
<snip>
A lot of the time it makes no difference. Also, sometimes it is worth
making non-portable assumptions.
Actually it does *not* depend on the representation for at least two
reasons.
1) As you have declared the variables as unsigned it makes no difference
which representation is used for signed arithmetic.
2) The C standard defined arithmetic on unsigned integers as wrapping in
the way you seem to expect. By the way, it defines the wrapping as not
being an overflow, a subtle point but important when reading the standard.
I've written code that depended on signed integers wrapping on overflow.
In the situation it was not unreasonable, but with hindsight I could
have done it using unsigned int instead and sidestepped the problem.
Some algorithms would have required a little tweaking, but nothing
difficult. I can't think of anything I have written since I left
assembler behind that depended on 2s complement though, apart from
reading numbers from an interface where the interface defined they were
2s complement.
--
Flash Gordon
David T. Ashley wrote, On 24/01/07 18:54:
<snip>
I've never actually seen a machine with an integer representation other than
2's complement. I'm not sure I could write portable code for such a
machine. I'm not sure I'd want to. With integers, knowledge of the
representation is baked into the way I think.
2's complement. I'm not sure I could write portable code for such a
machine. I'm not sure I'd want to. With integers, knowledge of the
representation is baked into the way I think.
making non-portable assumptions.
For example:
>
<BEGIN>
unsigned msb, lsb, thing_to_add;
>
lsb += thing_to_add;
>
if (lsb < thing_to_add)
msb++;
<END>
>
The code above works because it makes assumptions about how the integer is
represented and what will happen if you add too much to it.
>
<BEGIN>
unsigned msb, lsb, thing_to_add;
>
lsb += thing_to_add;
>
if (lsb < thing_to_add)
msb++;
<END>
>
The code above works because it makes assumptions about how the integer is
represented and what will happen if you add too much to it.
reasons.
1) As you have declared the variables as unsigned it makes no difference
which representation is used for signed arithmetic.
2) The C standard defined arithmetic on unsigned integers as wrapping in
the way you seem to expect. By the way, it defines the wrapping as not
being an overflow, a subtle point but important when reading the standard.
I don't really
want to program on machines where those fundamental assumptions about 2's
complement integer arithmetic don't hold.
want to program on machines where those fundamental assumptions about 2's
complement integer arithmetic don't hold.
In the situation it was not unreasonable, but with hindsight I could
have done it using unsigned int instead and sidestepped the problem.
Some algorithms would have required a little tweaking, but nothing
difficult. I can't think of anything I have written since I left
assembler behind that depended on 2s complement though, apart from
reading numbers from an interface where the interface defined they were
2s complement.
--
Flash Gordon
Comment