2 進数の 1 と 2 の補数

2 進数を文字列として指定すると、その 1 と 2 の補数を出力します。

1の補数 2 進数の は、その中のすべてのビットを切り替えることによって得られる別の 2 進数です。つまり、0 ビットを 1 に、1 ビットを 0 に変換します。1 の補数形式では、正の数は変化しません。負の数値は、対応する正の数値の 1 の補数をとることによって取得されます。

たとえば、+9 は 8 ビット表記で 00001001 として表され、-9 は 00001001 の 1 の補数である 11110110 として表されます。

例:

1's complement of '0111' is '1000' 1's complement of '1100' is '0011' 

2の補数 2 進数の 1 は 1 であり、2 進数の 1 の補数に加算されます。 2 進数の 2 の補数表現では、MSB はプラス記号に使用される「0」とマイナス記号に使用される「1」で符号を表します。残りのビットは大きさを表すために使用されます。正の大きさは、符号ビットまたは 1 の補数表現の場合と同じ方法で表現されます。負の大きさは、対応する正の値の 2 の補数で表されます。

例:

2's complement of '0111' is '1001' 2's complement of '1100' is '0100' 

2 の補数を見つけるためのもう 1 つのトリック:

ステップ1: 最下位ビットから開始して、1 が見つかるまで左に移動します。1 が見つかるまで、ビットは同じままです。

ステップ2: 1 を見つけたら、1 をそのままにして、今度は

ステップ 3: 残っているすべてのビットを 1 に反転します。

100100 の 2 の補数を見つける必要があるとします。

ステップ1: トラバースして、1 が見つかるまでビットを同じままにします。ここでは、x はまだ不明です。答え = xxxx00 –

ステップ2 : 1 を見つけました。そのままにしておきます。答え = xxx100

ステップ 3: 残りのビットをすべて 1 に反転します。答え = 011100。

したがって、100100 の 2 の補数は 011100 になります。

おすすめの練習法1の補足 やってみよう!

1 の補数の場合は、すべてのビットを反転するだけです。
2 の補数の場合、まず 1 の補数を見つけます。 LSB (最下位ビット) から始まる 1 の補数をたどって、0 を探します。0 が見つかるまですべての 1 を反転します (0 に変更します)。最後に、見つかった 0 を反転します。たとえば、01000 の 2 の補数は次のようになります。 11000 (最初に 01000 の 1 の補数が 10111 として見つかることに注意してください)。すべて 1 (1 の補数) がある場合は、文字列に 1 を追加します。たとえば、000 の 2 の補数は 1000 です (000 の 1 の補数は 111)。

以下は実装です。

C++




// C++ program to print 1's and 2's complement of> // a binary number> #include> using> namespace> std;> > // Returns '0' for '1' and '1' for '0'> char> flip(> char> c) {> return> (c ==> '0'> )?> '1'> :> '0'> ;}> > // Print 1's and 2's complement of binary number> // represented by 'bin'> void> printOneAndTwosComplement(string bin)> {> > int> n = bin.length();> > int> i;> > > string ones, twos;> > ones = twos => ''> ;> > > // for ones complement flip every bit> > for> (i = 0; i ones += flip(bin[i]); // for two's complement go from right to left in // ones complement and if we get 1 make, we make // them 0 and keep going left when we get first // 0, make that 1 and go out of loop twos = ones; for (i = n - 1; i>= 0; i--) { if (ones[i] == '1') twos[i] = '0'; else { twos[i] = '1'; 壊す; } } // ブレークがない場合: 111 または 11111 のようにすべて 1 です。 // このような場合、先頭に 1 を追加します if (i == -1) twos = '1' + twos; コート < < '1's complement: ' < < ones < < endl; cout < < '2's complement: ' < < twos < < endl; } // Driver program int main() { string bin = '1100'; printOneAndTwosComplement(bin); return 0; }>

ジャワ




// Java program to print 1's and 2's complement of> // a binary number> > class> GFG> {> > > // Returns '0' for '1' and '1' for '0'> > static> char> flip(> char> c)> > {> > return> (c ==> '0'> ) ?> '1'> :> '0'> ;> > }> > > // Print 1's and 2's complement of binary number> > // represented by 'bin'> > static> void> printOneAndTwosComplement(String bin)> > {> > int> n = bin.length();> > int> i;> > > String ones => ''> , twos => ''> ;> > ones = twos => ''> ;> > > // for ones complement flip every bit> > for> (i => 0> ; i { ones += flip(bin.charAt(i)); } // for two's complement go from right to left in // ones complement and if we get 1 make, we make // them 0 and keep going left when we get first // 0, make that 1 and go out of loop twos = ones; for (i = n - 1; i>= 0; i--) { if (ones.charAt(i) == '1') { twos = twos.substring(0, i) + '0' + twos.substring(i + 1); } else { twos = twos.substring(0, i) + '1' + twos.substring(i + 1); 壊す; } } // ブレークがない場合: 111 または 11111 のようにすべて 1 です。 // このような場合は、先頭に 1 を追加します if (i == -1) { twos = '1' + twos; System.out.println('1 の補数: ' + 1);; System.out.println('2 の補数: ' + twos); } // ドライバー コード public static void main(String[] args) { String bin = '1100'; printOneAndTwosComplement(bin); } } // このコードは Rajput-Ji によって提供されました>>'

>   




# Python3 program to print 1's and 2's> # complement of a binary number> > # Returns '0' for '1' and '1' for '0'> def> flip(c):> > return> '1'> if> (c> => => '0'> )> else> '0'> > # Print 1's and 2's complement of> # binary number represented by 'bin'> def> printOneAndTwosComplement(> bin> ):> > > n> => len> (> bin> )> > ones> => ''> > twos> => ''> > > # for ones complement flip every bit> > for> i> in> range> (n):> > ones> +> => flip(> bin> [i])> > > # for two's complement go from right> > # to left in ones complement and if> > # we get 1 make, we make them 0 and> > # keep going left when we get first> > # 0, make that 1 and go out of loop> > ones> => list> (ones.strip(''))> > twos> => list> (ones)> > for> i> in> range> (n> -> 1> ,> -> 1> ,> -> 1> ):> > > if> (ones[i]> => => '1'> ):> > twos[i]> => '0'> > else> :> > twos[i]> => '1'> > break> > > i> -> => 1> > # If No break : all are 1 as in 111 or 11111> > # in such case, add extra 1 at beginning> > if> (i> => => -> 1> ):> > twos.insert(> 0> ,> '1'> )> > > print> (> '1's complement: '> ,> *> ones, sep> => '')> > print> (> '2's complement: '> ,> *> twos, sep> => '')> > # Driver Code> if> __name__> => => '__main__'> :> > bin> => '1100'> > printOneAndTwosComplement(> bin> .strip(''))> > # This code is contributed> # by SHUBHAMSINGH10>

C#




// C# program to print 1's and 2's complement of> // a binary number> using> System;> > class> GFG> {> > > // Returns '0' for '1' and '1' for '0'> > static> char> flip(> char> c)> > {> > return> (c ==> '0'> ) ?> '1'> :> '0'> ;> > }> > > // Print 1's and 2's complement of binary number> > // represented by 'bin'> > static> void> printOneAndTwosComplement(String bin)> > {> > int> n = bin.Length;> > int> i;> > > String ones => ''> , twos => ''> ;> > ones = twos => ''> ;> > > // for ones complement flip every bit> > for> (i = 0; i { ones += flip(bin[i]); } // for two's complement go from right to left in // ones complement and if we get 1 make, we make // them 0 and keep going left when we get first // 0, make that 1 and go out of loop twos = ones; for (i = n - 1; i>= 0; i--) { if (ones[i] == '1') { twos = twos.Substring(0, i) + '0' + twos.Substring(i + 1,twos.Length-( i+1)); } else { twos = twos.Substring(0, i) + '1' + twos.Substring(i + 1,twos.Length-(i+1)); 壊す; } } // ブレークがない場合: 111 または 11111 のようにすべて 1 です。 // このような場合は、先頭に 1 を追加します if (i == -1) { twos = '1' + twos; Console.WriteLine('1 の補数: ' + 1);; Console.WriteLine('2 の補数: ' + 2); } // ドライバー コード public static void Main(String[] args) { String bin = '1100'; printOneAndTwosComplement(bin); } } // このコードは 29AjayKumar によって寄稿されました>>

JavaScript




> > // Javascript program to print 1's and 2's complement of> // a binary number> > // Returns '0' for '1' and '1' for '0'> function> flip (c) {> return> (c ==> '0'> )?> '1'> :> '0'> ;}> > // Print 1's and 2's complement of binary number> // represented by 'bin'> function> printOneAndTwosComplement(bin)> {> > var> n = bin.length;> > var> i;> > > var> ones, twos;> > ones = twos => ''> ;> > > // for ones complement flip every bit> > for> (i = 0; i ones += flip(bin[i]); // for two's complement go from right to left in // ones complement and if we get 1 make, we make // them 0 and keep going left when we get first // 0, make that 1 and go out of loop twos = ones; twos = twos.split('') for (i = n - 1; i>= 0; i--) { if (ones[i] == '1') twos[i] = '0'; else { twos[i] = '1'; 壊す; } } twos = twos.join('') // ブレークがない場合、111 または 11111 のようにすべて 1 になります。 // このような場合、先頭に 1 を追加します if (i == -1) twos = '1' + twos; document.write( '1 の補数: ' + 1 + ' '); document.write( '2 の補数: ' + twos + ' '); } // ドライバー プログラム var bin = '1100'; printOneAndTwosComplement(bin);>>

出力:

1's complement: 0011 2's complement: 0100 

時間計算量: の上)

補助スペース: ○(1)