番号が長いです。さて、私がやりたいのは次のとおりです。
long l = "001000......10001100000" (bits representation)
左から3番目と4番目のビット(つまり10)を削除し、残りの62ビットをlongに変換する必要があります
long newl = "0000......10001100000" (bits representation)
誰かが私がJavaでこれを効率的に行うのを手伝ってくれる?
番号が長いです。さて、私がやりたいのは次のとおりです。
long l = "001000......10001100000" (bits representation)
左から3番目と4番目のビット(つまり10)を削除し、残りの62ビットをlongに変換する必要があります
long newl = "0000......10001100000" (bits representation)
誰かが私がJavaでこれを効率的に行うのを手伝ってくれる?
通常、ビットをマスクで設定します。マスクの補数を使用してビットOR
をクリアします。AND
long mask = 3L << 60; // 001100...
long newl = l & ~mask; // Clear the bits in the mask.
ビットを削除する(そして効果的に値を62ビットに短縮する)ことを検討している場合は、次のことを試すことができます。
long topBits = ((3L << 62) & l) >> 2; // Top 2 bits, shifted right
long bottomBits = (~(15L << 60) & l); // Bottom 60 bits
long newl = topBits | bottomBits;
文字列としてビットのリストがある場合は、その文字列から3番目と4番目のビットを削除してから、を使用しますLong.parseLong(string, 2)
。