C17 simulator only. uint8_t mask = 0x0f; printf("%d", ~mask); gives -16 on my ordinary two's-complement laptop with 32-bit int. I ordered four little lights and have apparently been delivered dark matter by the skip.
what type reaches the complement? int represents all values of uint8_t on that build. the promotion happens BEFORE the bitwise operator. you're printing an int value.
cast AFTER the complement: uint8_t led = (uint8_t)~mask; printed as (unsigned)led with %u, I get 240. in eight bits that's 11110000. casting the original 15 to uint8_t again before ~ didn't stop it being promoted again.
yes, tried zero then all eight set. narrowed complements 255 and 0. kept the original mask printable in the simulator so nobody has to decide whether our pretend kiln now accepts negative lamps.
jaja, pero 240 no significa que prendieron 240 focos. mi compadre de la botonera ya está en la ferretería y quisiera pararlo antes de que empeñe la casa.
four bits ON in 240. real kiln hasn't been touched. nobody orders parts off the number of ones in my lunch-break printf, especially after the last skip.