0

I have the following struct:

struct holder {
  int cause;
  int agent:1, group:1, supervisor:1, defer:1;
};

Klocwork complains at the int agent:1 ... line saying: Bit field 'agent' has one bit and is signed. Signed one bit field, CWARN.SIGNEDBIT.

I don't understand why the integer being signed would make any difference. I realise that for a signed integer the leftmost (most significant) bit is used to determine whether integer is positive or negative but in case of bit fields that surely wouldn't matter???

Klocwork developer web site says:

Signed bitfields require at least two bits, and the two possible values of the field are -1 and 0. Although it is safe to check a 1-bit signed bitfield for 0, using it as a Boolean flag, other arithmetic operations may yield unexpected results.

Can someone please explain why integer must be unsigned in this case?

Angus Comber
  • 9,316
  • 14
  • 59
  • 107
  • a) C doesn't define the size of `int` in deterministic and universal terms, in other words you can't assume that the size of `int` is N bits b) C doesn't define endianess, you can't assume any particular representation for the `int` data type. 3) do yourself a favor and go for a good book or drop this really bad idea about bitfields . – user2485710 May 07 '14 at 08:48
  • @user2485710, bitfields are not a bad idea. They are in fact extremely useful in embedded programming. There are cases where they still make sense even in higher level programs, but they are not as necessary there. – Shahbaz May 07 '14 at 08:52
  • @Shahbaz if you are missing the basics about `int` you don't go for the bitfields, you are shooting yourself in the foot, both foots, the entire body. bitfields can be involved in all kinds of issues, from the standard related definitions to the ABIs. – user2485710 May 07 '14 at 08:55
  • @user2485710, from your questions and answers I assume you are mostly a C++ developer, so most likely with very little if any at all experience in embedded programing in C. This is actually a common phenomenon: when you start learning you think you know nothing, then at some point you feel you know _everything_, until when you learn more deeply that you realize you don't know anything. I'm guessing you are in the second phase. – Shahbaz May 07 '14 at 09:03
  • @Shahbaz I still think that bitfields are a bad idea with this premises though . I know that this is a common solution in the embedded world, like `union` are, but I don't know how far you can go if you assume that the C standard gives you a fixed size for `int` with an endianess defined. – user2485710 May 07 '14 at 09:14
  • @user2485710, _but I don't know how far you can go if you assume that the C standard gives you a fixed size for `int`_, perhaps you got me wrong. You never go assuming `int` has a fixed size, unless you are coding for a specific processor. – Shahbaz May 07 '14 at 11:45

1 Answers1

2

If you use the value as a flag, it's more logical to store it as unsigned since the normal expression would be 1 and 0, not -1 and 0. A logic expression in C also returns 1 if true, not -1

Another problem is that if you use signed bit fields, it may take many more instructions to sign extend the value instead of just masking off the unneeded bits.

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • small correction: `true = non zero`, `false = zero`, since `-1` is a non zero value, it's `true` by definition. – user2485710 May 07 '14 at 08:55
  • @user2485710 I think you missed the point of the answer. What Luu is saying is that if you ever do: `(some logical expression) == (your signed 1-bit flag)`, you may not get the expected result because the logical expression would return 1 if true, but your signed 1-bit flag would evaluate to -1, which aren't equal. – Shahbaz May 07 '14 at 09:07
  • @Shahbaz my doubts start with **`A logic expression in C`...**, if you are talking about a logical expression than this definition applies. It depends what the answer is really about. – user2485710 May 07 '14 at 09:11
  • @user2485710 I mean a logical expression. "The logic expressin in C" applies to the latter statement – phuclv May 07 '14 at 09:50