News for hackers. Stuff that matters.
Re:Integers in C quiz by veltas on Sunday September 04, 2022 @09:22AM (#32712759)
Believe it or not, this makes the behavior more like what you'd expect in many cases. For example:

  uint8_t x = 4;
  extern volatile uint64_t *reg;
  *reg &= ~x;
In the last statement x is promoted to an int, and then when the logical NOT occurs every bit is set to 1, including the high bit. When it's converted to a uint64_t for the AND, the high bits are also set to 1. So the result is that the final statement clears only bit 2 in *reg.

If it promoted to unsigned int, then it would also clear bits 32-63.

Re:Integers in C quiz by tialaramex on Sunday September 04, 2022 @01:55PM (#32715724)

  extern volatile uint64_t *reg;
  *reg &= ~x;
People should stop doing this. What this means is:

  extern volatile uint64_t *reg;
  uint64_t tmp = *reg;
  tmp &= ~x;
  *reg = temp;
But of course when you write that chances are somebody will point out that you're running in interruptible context sometimes in this function, so that's actually introducing a race condition. Why didn't they say so when you wrote it your way? Because that looked like a single operation and so it wasn't obvious it might get interrupted.
Re:Integers in C quiz by codeflo on Sunday September 04, 2022 @10:44AM (#32713599)
I don’t find that very convincing. It’s simply ambiguous, I might want this:

    *reg &= ~(uint64_t)x;
or, and there’s no elegant way to even write this in C, I might want:

    *reg &=(uint64_t)(uint8_t)~x;
The fact that I have to write two casts here to undo the damage of the auto-promotion is evidence of how broken this is.
Re:Integers in C quiz by veltas on Sunday September 04, 2022 @01:29PM (#32715449)
That second line can be written:

  *reg &= (uint8_t)~x;
Or:

  *reg &= ~x & 0xFF;