Re: !!, what is it?



Tim Woodall wrote:
On Mon, 12 Sep 2005 09:36:01 +0200,
    Michael Mair <Michael.Mair@xxxxxxxxxxxxxxx> wrote:

As an aside:
I recall seeing some platform specific headers which went for the
all-bits-one representation of "true" -- but the C implementations
gave 1 for !!TRUE as well...

int is_it_seven(int x) { return x==7; }

if(is_it_seven(7) == TRUE)
    printf("7 is seven\n");
else
    printf("7 is not seven\n");

While I would never write the explicit test for TRUE [1], I would be
horrified at any header that defined TRUE such that this code didn't
behave as expected.

[1] Other peoples coding standards excepted.

Sorry, without C99's _Bool and _True, this argument is bogus. Apart from the possible range of return values one could expect from is_it_seven() and the possible mismatch with, say isalnum() == TRUE or strcmp() != TRUE, there is no benefit in that. IIRC, this "TRUE" was intended for bitwise operations and conveniently fulfilled !TRUE == FALSE. However, this would not have worked on a 1s complement platform.

One other explanation for this misunderstanding is that you may have been using
1 bit bitfields: it is implementation defined whether these have values 0 and 1
or 0 and -1.  As far as I recall, C99 did not change that.

Naive question after having a look at C99, 6.7.2.1: Does this ambiguity only hold for "int" bitfields or also for "signed int" bitfields?

If you are using sign-magnitude or ones complement encoding then the possible values of a signed int bitfield will be +0 and -0 or 0 and a trap representation.

(I hope there isn't something somewhere that says that bitfields must be
twos complement. I can't see it but I'm sure if there is someone here
will be along quickly to correct me. ;-) )

Sorry, I probably did not phrase it carefully enough.

This was not my question. I am well aware of s-m and 1s complement,
my question more or less is whether it is possible that a signed
int bitfield could be treated as if it was an unsigned int
bitfield?

Here some context; see especially the footnote:

"
6.7.2.1:

4 A bit-field shall have a type that is a qualified or unqualified
version of _Bool, signed int, unsigned int, or some other implementation-defined type.


Semantics
[...]
8 A member of a structure or union may have any object type other
than a variably modified type.102)
In addition, a member may be declared to consist of a specified
number of bits (including a sign bit, if any). Such a member is
called a bit-field;103) its width is preceded by a colon.
9 A bit-field is interpreted as a signed or unsigned integer type
consisting of the specified number of bits.104) If the value 0 or
1 is stored into a nonzero-width bit-field of type _Bool, the
value of the bit-field shall compare equal to the value stored.
____
104) As specified in 6.7.2 above, if the actual type specifier
used is int or a typedef-name defined as int, then it is
implementation-defined whether the bit-field is signed or unsigned.
"

Cheers
 Michael
--
E-Mail: Mine is an   /at/ gmx /dot/ de   address.
.



Relevant Pages

  • Re: Bit field types and promotions
    ... > I presume you meant where n is more than the bits in an unsigned int. ... since bitfields of type larger than signed/unsigned int are ... > no promotion occurs in the expression ... ....results in the 40 bit bit-field being promoted to a 50 bit unsigned ...
    (comp.std.c)
  • Re: Bit-fields and integral promotion
    ... > First, bitfields larger than an int are not allowed, and they are ... > not allowed to cross int boundaries, ... large enough to hold a bit-field. ... If insufficient space remains, whether a bit-field that ...
    (comp.lang.c)
  • Re: Colon initialiser?
    ... "A bit-field shall have a type that is a qualified or unqualified ... version of one of int, unsigned int, or signed int. ... bitfields can be signed int, ... qualifier "signed" is used explicitly, one of the bits is a sign ...
    (comp.lang.c)
  • Re: Bit-fields and integral promotion
    ... >> be represented by an int, I'd expect it to get promoted to int, ... The definition of integer promotion say quite specifically that for integers ... On the other hand, 6.7.2.1p4 says, "A bit-field shall have a type ... about "a bit-field of type _Bool, int, signed int, or unsigned int". ...
    (comp.lang.c)
  • Re: portable and efficient signed bitfields
    ... static unsigned int store_signed ... static signed int load_signed ... Using bitfields I can get rid of the excessive bitwise manipulations ... Whether the high-order bit position of a ``plain'' int bit-field is ...
    (comp.lang.c)