Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
符号ビットを持つ負の 2 進数があり、そのグレイ コードを取得するプログラムを作成したいと考えています。ただし、正の数の解しか見つかりません。だからここで私はこの質問をしています。ありがとう。
グレイ コードは、次の方法を使用して、負でない数値に対してのみ計算できます。
int gray_encode(int n) { return n ^ (n >> 1); }
2 進数は 2 の補数表現であるため、同じ方法は負の数に対しては機能しません。