M
Mantorok Redgormor
are all integers represented internally as just bit vectors?
-- nethlek
-- nethlek
Mantorok Redgormor said:are all integers represented internally as just bit vectors?
Mantorok said:are all integers represented internally as just bit vectors?
are all integers represented internally as just bit vectors?
-- nethlek
Basically yes. You could build a computer that doesn't use binary, but thenMantorok Redgormor said:are all integers represented internally as just bit vectors?
ISO C does not define how integers are represented internally.
machine.Malcolm said:Basically yes. You could build a computer that doesn't use binary, but then
C wouldn't be a good language to use with it.
The vast majority of systems use two's complement notation for negative
numbers, eg -1 = all bits set. C doesn't actually require this but most real
C programs would probably break if ported to a non-two's complement
Joona I Palaste said:ISO C does not define how integers are represented internally.
Malcolm said:The vast majority of systems use two's complement notation for negative
numbers, eg -1 = all bits set. C doesn't actually require this but most real
C programs would probably break if ported to a non-two's complement machine.
In said:ISO C does not define how integers are represented internally.
Dan Pop said:In <[email protected]> Joona I Palaste
If you were right, a Gray code-based implementation would be perfectly
conforming. Let's see if you're right:
That's quite a lot of text about how integers are represented and,
incidentally, it rules out Gray coded integers...
Glen Herrmannsfeldt said:I think there was a discussion some time ago on the use of BCD
representation. Following the "as if" rule, I believe others said it was
possible. It won't be easy, for example, to make shift operators, or bit
and/or/xor work right.
I would say that the "as if" rule applies here, too. One could, for
example, store numbers in gray code in memory but convert to one of the
standard forms in registers. (More popular is to use ECC coding in memory
and not in registers.) If you didn't do that, arithmetic operations would
be complicated. (I haven't thought about doing addition in gray code, and
multiplication would be even worse.)
I believe also byte and bit order in memory is not defined by
the standard. While big and little endian are popular, others
are possible.
If a machine did store integers of different sizes in memory
using gray code, the results of accessing parts of such numbers
would be very different than the normal big/little endian
results, but I would say legal within the standard.
But yes, all the C operators must supply results following
the standard, which mostly covers the possibilities of twos
complement, ones complement (CDC and Univac that I know of),
and sign magnitude (the last I know of is the 7090).
If the "internally" in the OP question means "in memory" then
I would say that it could be done.
We tend to develop games using a PC game editor, for a console target. TheJames Dow Allen said:Perhaps you can post a suspicious *common* code example.
Malcolm said:We tend to develop games using a PC game editor, for a console target. The
editor spits out a binary file, and the console reads it. I often provide
two functions, fput16() and fget16() to write and read 16-bit integers.
fget16() sign-extends the number it reads, so will break on a non-two's
complement machine.
Now strictly we should be using text to exchange data between platforms, but
that's not the real world. Strictly I suppose I could write fget16() so that
it didn't break on a one's complement machine, but that's hardly worth
doing.
Simon Biber said:The standard does not differentiate between register and memory storage, so
the as if rule could work there. However, it does allow you to inspect the
object representation using, for example, an array of unsigned chars. If you
inspect the representation of an integer in that way, it MUST conform to the
binary representation specified.
This should output the object representation in binary of various
values of an integer type given 'type' and 'TYPE_MAX' are defined.
It would be non-conforming if this output represented anything
other than value bits, padding bits and sign bit. (In any order.)
This is true, the order of the various bits is not specified, which
allows big and little endian and other orderings. However, there
is not enough latitude to allow a gray code encoding.
I would say illegal.
Malcolm said:... I suppose I could write fget16() so that it didn't break on a
one's complement machine, but that's hardly worth doing.
(snip of quotes from some version of the C standard.)
I think there was a discussion some time ago on the use of BCD
representation. Following the "as if" rule, I believe others said it was
possible. It won't be easy, for example, to make shift operators, or bit
and/or/xor work right.
Malcolm said:I often provide
two functions, fput16() and fget16() to write and read 16-bit integers.
fget16() sign-extends the number it reads, so will break on a non-two's
complement machine.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.