how to take twos complement of a byte in c++
问题 I am looking at some c++ code and I see: byte b = someByteValue; // take twos complement byte TwosComplement = -b; Is this code taking the twos complement of b? If not, What is it doing? 回答1: This code definitely does compute the twos-complement of an 8-bit binary number, on any implementation where stdint.h defines uint8_t : #include <stdint.h> uint8_t twos_complement(uint8_t val) { return -(unsigned int)val; } That is because, if uint8_t is available, it must be an unsigned type that is