What is “2's Complement”?

前端 未结 23 2486
名媛妹妹
名媛妹妹 2020-11-21 05:59

I\'m in a computer systems course and have been struggling, in part, with Two\'s Complement. I want to understand it but everything I\'ve read hasn\'t brought the p

23条回答
  •  别那么骄傲
    2020-11-21 06:16

    REFERENCE: https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html

    I invert all the bits and add 1. Programmatically:

      // in C++11
      int _powers[] = {
          1,
          2,
          4,
          8,
          16,
          32,
          64,
          128
      };
    
      int value=3;
      int n_bits=4;
      int twos_complement = (value ^ ( _powers[n_bits]-1)) + 1;
    

提交回复
热议问题