<< Back to previous view

[PCC-460] issue with bitwise negation Created: 03/Aug/14  Updated: 03/Aug/14

Status: Resolved
Project: pcc
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major
Reporter: Volkmar Klatt Assignee: Anders Magnusson
Resolution: Fixed Votes: 0
Environment: pcc 1.1.0.DEVEL 20140729 for i686-pc-linux-gnu

File Attachments: Text File tr_20140731_46.c    

/* tr_20140731_46.c - test file for pcc, Volkmar Klatt

   The bitwise negation of an
   unsigned bitfield var
   gives an integer (and so may overflow)

   I was wondering if this is OK, as C says nothing about
   the representation of bits in bitfields -
   then it's just an incompatility with gcc, clang, cparser, ...

   on the other hand:
  "for an unsigned operand e, ~e has the value
   UINT_MAX - e if the converted type of e is unsigned, or
   ULONG_MAX -e if the converted type of e is unsigned long."
       Harbison Steele, Section 7.5 (Unary Expressions, p. 223 in edition 5)
#include <stdio.h>

struct S {
   unsigned f : 22;

struct S s = {1};

int main(void)
    int x;

    x = ((~s.f) < 0);
    printf("%i\n", x);

    printf("%i\n", ~s.f);
    printf("%u\n", ~s.f);

    return 0;

Comment by Anders Magnusson [ 03/Aug/14 07:12 PM ]
Hm, interesting case. I think this is a pcc bug; integer promotions should be performed:
"If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int."
Which means that if this bitfield is less than 32 it will be converted to an int.
Comment by Anders Magnusson [ 03/Aug/14 07:49 PM ]
Now integer promotions is added to bitfields also :-)
Generated at Fri Oct 20 16:22:58 CEST 2017 using JIRA Enterprise Edition, Version: 3.13.1-#333.