Re: !!, what is it?
- From: Tim Rentsch <txr@xxxxxxxxxxxxxxxxxxx>
- Date: 14 Sep 2005 15:16:20 -0700
Tim Woodall <devnull@xxxxxxxxxxxxx> writes:
> On Tue, 13 Sep 2005 09:12:42 +0200,
> Michael Mair <Michael.Mair@xxxxxxxxxxxxxxx> wrote:
[snip]
> > 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?
> >
> No. (IMO) 6.7.2.1
>
> 9 A bit-field is interpreted as a signed or unsigned integer type ...
>
> and then footnote 104) paraphrased - if the type specifier is int it is
> implementation defined whether the bitfield is signed or unsigned
>
> Which implies to me that signed int -> signed, unsigned int -> unsigned
> and int goes to one or the other.
It is possible for a signed int bitfield to behave as though
it holds an unsigned int bitfield value.
On a one-bit bitfield, for example, an implementation could
define 0 as "zero" and 1 as "trap representation". Storing
any non-zero value into the bitfield would result in a trap
representation, which can do anything because of undefined
behavior. In particular, storing a 1 could later result in
a 1 being produced.
A multi-bit bitfield could have all "negative values" be
trap representations. Sort of weird, but it's allowed. At
least, I haven't found any language in the Standard that
disallows it. The particular case of a non-zero being
stored in a 1-bit bitfield is explicitly allowed (in section
6.2.6.2 p2); it does at least have to be explicitly
identified by the implementation.
Because of the wonders of undefined behavior, it's also
possible to mix and match. For example:
struct {
signed int a:1;
signed int b:1;
} bits;
bits.a = 1;
bits.b = -1;
printf( "a: %d b: %d\n", bits.a, bits.b );
could very well print "a: 1 b: -1" as its output. Something
like this might happen if a compiler did some dataflow
analysis and determined that the 'a' field is used to hold
1's and 0's and the 'b' field is used to hold 0's and -1's.
Once some evaluation results in a trap representation being
stored, any behavior is possible -- it doesn't have to be
consistent from variable to variable.
.
- References:
- !!, what is it?
- From: bjk of course
- Re: !!, what is it?
- From: Mike Wahler
- Re: !!, what is it?
- From: Cafer Şimşek
- Re: !!, what is it?
- From: Flash Gordon
- Re: !!, what is it?
- From: Kenneth Brody
- Re: !!, what is it?
- From: Charlie Gordon
- Re: !!, what is it?
- From: Michael Mair
- Re: !!, what is it?
- From: Tim Woodall
- Re: !!, what is it?
- From: Michael Mair
- Re: !!, what is it?
- From: Tim Woodall
- !!, what is it?
- Prev by Date: Re: 1 more doubt !
- Next by Date: Re: 2 doubts !
- Previous by thread: Re: !!, what is it?
- Next by thread: Re: !!, what is it?
- Index(es):
Relevant Pages
|