A signed bit field of 1 bit length is not useful

6.5 Identifiers

Moderators: misra-c, david ward

Post Reply
JonM
Posts: 8
Joined: Wed Apr 25, 2007 9:03 am
Location: Warwickshire

A signed bit field of 1 bit length is not useful

Post by JonM » Sun Feb 08, 2009 8:41 am

Is it possible to improve Rule 6.5 "A signed bit field of 1 bit length is not useful". Unlike the other rules, this doesn't actually advise/recommend any behaviour or give any insight as to what compilers may or may not do when presented with this.

How about - following the same outline structure of the following rule 7.1, something like:

A signed bit field of 1 bit length is treated as ...

There is a danger of ...

They are problematic when ...

It is better not to use signed bit fields of 1 bit length use ...

Jonathan

Lundin
Posts: 70
Joined: Mon Dec 10, 2007 1:57 pm

Re: A signed bit field of 1 bit length is not useful

Post by Lundin » Fri Feb 13, 2009 3:17 pm

The rule is sort of sarcastic... It assumes you know enough of programming to just laugh and nod when you read it. What you can read between the lines is that the sign bit itself takes up one bit alone, and that it doesn't make sense to create a variable which is either "minus" or "not minus".

Also, the rule can't advise of how a compiler will react when presented with such a variable, since bit-fiends are incredibly poorly specified by the standard. Everything about them is unspecified- or implementation-specific behavior... one's complement or two's complement? First bit of the struct, lsb or msb? Big or little endian? Then there are non-standard extensions that allow other types than int...

misra-c
Posts: 569
Joined: Thu Jan 05, 2006 1:11 pm

Re: A signed bit field of 1 bit length is not useful

Post by misra-c » Mon Mar 30, 2009 11:23 am

The use of a single bit is illogical on a target adopting two’s complement representation. The intention of the programmer may have been to allow for values of -1 and +1 (as well as zero), but such data values clearly require at least 2 bits (which in turn actually provides a range of -2 to +1). The ISO C standard does not define the required behaviour for a signed bit field of length 1.

Tracker ID: 17
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “6.5 Identifiers”