You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Marc Slemko <ma...@worldgate.com> on 1998/03/07 06:55:24 UTC

bitfields and negative

if you have a bitfield:

    int double_reverse:2;       /* have we done double-reverse DNS?
                                 * -1 yes/failure, 0 not yet, 1 yes/success */

is it really valid to set it to -1 or does it just work without being
quite valid?

(I'm not arguing that it doesn't work)

Why are some of those just done as "unsigned" while some are just "int"?
Shouldn't they all be unsigned?


Re: bitfields and negative

Posted by Ben Laurie <be...@algroup.co.uk>.
Dean Gaudet wrote:
> 
> On Sat, 7 Mar 1998, Ben Laurie wrote:
> 
> > What, in a bitfield? News to me... but you appear to be right. K&R say
> > "for portability, specify signed and unsigned explicitly". Damned stupid
> > thing to say, but there we are.
> 
> Interesting... I didn't know that either.  I guess a bunch of them need to
> be cleaned up then.

Tedious, but +1.

Cheers,

Ben.

-- 
Ben Laurie            |Phone: +44 (181) 735 0686|  Apache Group member
Freelance Consultant  |Fax:   +44 (181) 735 0689|http://www.apache.org
and Technical Director|Email: ben@algroup.co.uk |
A.L. Digital Ltd,     |Apache-SSL author    http://www.apache-ssl.org/
London, England.      |"Apache: TDG" http://www.ora.com/catalog/apache

Re: bitfields and negative

Posted by Dean Gaudet <dg...@arctic.org>.

On Sat, 7 Mar 1998, Ben Laurie wrote:

> What, in a bitfield? News to me... but you appear to be right. K&R say
> "for portability, specify signed and unsigned explicitly". Damned stupid
> thing to say, but there we are.

Interesting... I didn't know that either.  I guess a bunch of them need to
be cleaned up then. 

Dean


Re: bitfields and negative

Posted by Ben Laurie <be...@algroup.co.uk>.
Marc Slemko wrote:
> 
> On Sat, 7 Mar 1998, Ben Laurie wrote:
> 
> > typedef int BOOL;
> >
> > struct thing
> >       {
> >       BOOL x:1;
> >       };
> >
> > BOOL Compare(struct thing *p,BOOL b)
> >       { return p->x == b; }
> >
> > Why? Coz TRUE is 1, but a one bit signed bitfield can only have values 0
> > and -1.
> 
> My understanding was that some compilers default to signed, some default
> to unsigned if it is not specified...

What, in a bitfield? News to me... but you appear to be right. K&R say
"for portability, specify signed and unsigned explicitly". Damned stupid
thing to say, but there we are.

Cheers,

Ben.

--
Ben Laurie                Phone: +44 (171) 460 4460
Lead Architect            Fax:   +44 (171) 460 4461
Orchestream Ltd.
London, England.

-- 
Ben Laurie            |Phone: +44 (181) 735 0686|  Apache Group member
Freelance Consultant  |Fax:   +44 (181) 735 0689|http://www.apache.org
and Technical Director|Email: ben@algroup.co.uk |
A.L. Digital Ltd,     |Apache-SSL author    http://www.apache-ssl.org/
London, England.      |"Apache: TDG" http://www.ora.com/catalog/apache

Re: bitfields and negative

Posted by Marc Slemko <ma...@worldgate.com>.
On Sat, 7 Mar 1998, Ben Laurie wrote:

> typedef int BOOL;
> 
> struct thing
> 	{
> 	BOOL x:1;
> 	};
> 
> BOOL Compare(struct thing *p,BOOL b)
> 	{ return p->x == b; }
> 
> Why? Coz TRUE is 1, but a one bit signed bitfield can only have values 0
> and -1.

My understanding was that some compilers default to signed, some default
to unsigned if it is not specified...


Re: bitfields and negative

Posted by Ben Laurie <be...@algroup.co.uk>.
Marc Slemko wrote:
> 
> if you have a bitfield:
> 
>     int double_reverse:2;       /* have we done double-reverse DNS?
>                                  * -1 yes/failure, 0 not yet, 1 yes/success */
> 
> is it really valid to set it to -1 or does it just work without being
> quite valid?

It is valid.

> (I'm not arguing that it doesn't work)
> 
> Why are some of those just done as "unsigned" while some are just "int"?
> Shouldn't they all be unsigned?

Pass. But they should probably be unsigned. I've been bitten by this in
the past:

typedef int BOOL;

struct thing
	{
	BOOL x:1;
	};

BOOL Compare(struct thing *p,BOOL b)
	{ return p->x == b; }

Why? Coz TRUE is 1, but a one bit signed bitfield can only have values 0
and -1.

Cheers,

Ben.

-- 
Ben Laurie            |Phone: +44 (181) 735 0686|  Apache Group member
Freelance Consultant  |Fax:   +44 (181) 735 0689|http://www.apache.org
and Technical Director|Email: ben@algroup.co.uk |
A.L. Digital Ltd,     |Apache-SSL author    http://www.apache-ssl.org/
London, England.      |"Apache: TDG" http://www.ora.com/catalog/apache

Re: bitfields and negative

Posted by Marc Slemko <ma...@worldgate.com>.
On Fri, 6 Mar 1998, Marc Slemko wrote:

> if you have a bitfield:
> 
>     int double_reverse:2;       /* have we done double-reverse DNS?
>                                  * -1 yes/failure, 0 not yet, 1 yes/success */
> 
> is it really valid to set it to -1 or does it just work without being
> quite valid?
> 
> (I'm not arguing that it doesn't work)
> 
> Why are some of those just done as "unsigned" while some are just "int"?
> Shouldn't they all be unsigned?

Erm... I mean signed.

>