how to take twos complement of a byte in c++

后端 未结 3 730
清歌不尽
清歌不尽 2020-12-17 01:42

I am looking at some c++ code and I see:

 byte b = someByteValue;
 // take twos complement
 byte TwosComplement = -b;

Is this code taking t

3条回答
  •  孤城傲影
    2020-12-17 01:55

    The correct expression will look

    byte TwosComplement = ~b + 1;
    

    Note: provided that byte iis defined as unsigned char

提交回复
热议问题