[comp.lang.c] representation of integers

karl@ima.isc.com (Karl Heuer) (11/05/90)

In article <1990Nov3.204327.19057@NCoast.ORG> catfood@NCoast.ORG (Mark W. Schumann) writes:
>You are simply not allowed to assume *anything* about the internal
>representation of integers if your programs are to be 100% portable ANSI.

Actually, the Standard guarantees that bitstrings with the sign bit clear
will have the obvious interpretation as a nonnegative integer.  When the sign
bit is set things get a bit murkier: apparently the Committee intended that
two's complement, one's complement, and sign-magnitude representations are all
legal.

The relevant rule says something about "strict binary except for the sign
bit".  One interpretation would be that this says nothing at all when the sign
bit is set, and so you could have something silly like normal binary for
positives and Gray code for negatives.

Alternately, it could mean that a bitstring b where b&HIBIT is set has the
value M*(b&~HIBIT)+A for some constants M and A.  This, together with the
required limit values (short int must include the range [-32767,+32767], etc.)
would imply that the only legal representations are the above three and a
skewed sign-magnitude (M=-1, A=-1).

A related issue is the question of whether it's required for U{type}_MAX+1 to
be a power of two, and if so, whether it must be 1<<sizeof(type)*CHAR_BIT
(false on some Cray machines, I'm told).

These are on my list of questions to send X3J11 for interpretation someday.
If somebody else gets around to it first, that's fine.

Karl W. Z. Heuer (karl@ima.isc.com or uunet!ima!karl), The Walking Lint
Followups to comp.std.c.

jamiller@hpcupt1.cup.hp.com (Jim Miller) (11/07/90)

>In article <226@smds.UUCP> rh@smds.UUCP (Richard Harter) writes:
>> 	for (y=x,sqrtx=1;y>0;) {ysave = y;y =/ 16;sqrtx =* 4;}
>
>Wow.
>
>This man has been programming in C for a *long* time. I'm impressed.
>
>(quiz: why do I come to this conclusion?)
>-- 
>Peter da Silva.   `-_-'

Because:
>> 	for (y=x,sqrtx=1;y>0;) {ysave = y;y =/ 16;sqrtx =* 4;}
                                            ^^
   Is not ANSII, and has been considered bad form for years and years and ...

   On the other hand, I wonder if maybe he is actually very young and
   still makes typo's -- unlike us old coots  :-)

   jim miller
   jamiller@hpmpeb7.cup.hp.com
   (a.k.a James A. Miller; Jim the JAM; stupid; @!?$$!; ... )
   Anything I say will be used against me ...
   But my company doesn't know or approve or condone anything of mine here.

brianh@hpcvia.CV.HP.COM (brian_helterline) (11/09/90)

jamiller@hpcupt1.cup.hp.com (Jim Miller) writes:

>>In article <226@smds.UUCP> rh@smds.UUCP (Richard Harter) writes:
>>> 	for (y=x,sqrtx=1;y>0;) {ysave = y;y =/ 16;sqrtx =* 4;}
>>
>>Wow.
>>
>>This man has been programming in C for a *long* time. I'm impressed.
>>
>>(quiz: why do I come to this conclusion?)
>>-- 
>>Peter da Silva.   `-_-'

>Because:
>>> 	for (y=x,sqrtx=1;y>0;) {ysave = y;y =/ 16;sqrtx =* 4;}
                                            ^^
>   Is not ANSII, and has been considered bad form for years and years and ...
           ^^^^^
>   On the other hand, I wonder if maybe he is actually very young and
>   still makes typo's -- unlike us old coots  :-)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      What is ANSII? a typo? or the ASCII version of ANSI? ;)