2

使用しているサードパーティ関数に対してロジックの一部を再確認したいのですが、ビット単位のロジックが正しく理解されているかどうかわかりません。各シナリオで変数「intValue」の値の範囲を教えてもらえますか?これにより、各条件がtrueを返しますか?ありがとう!

        if ((intValue < 0 && ((intValue & 0xFFFFFF80) == 0xFFFFFF80)) ||
            (intValue & 0x0000007F) == intValue) {

        }
        else if ((intValue < 0 && ((intValue & 0xFFFF8000) == 0xFFFF8000)) ||
            (intValue & 0x00007FFF) == intValue) {

        }
        else if ((intValue < 0 && ((intValue & 0xFF800000) == 0xFF800000)) ||
            (intValue & 0x007FFFFF) == intValue) {

        }
        else {

        }
4

2 に答える 2

2
if (intValue == (SByte)intValue) {
     // matches -128...127, i.e. values that fit in one byte
}
else if (intValue == (Int16)intValue) {
     // matches -32768..32767, values that fit in two bytes
}
else if (intValue == ((intValue << 8) >> 8)) {
     // matches -2**23..(2**23-1), values that fit in three bytes
}
else {
     // matches -2**31..(2**31-1), values that need four bytes
}

intValue < 0変数タイプが署名されている場合、すべてのテストは完全に冗長であることに注意してください。

于 2011-07-08T23:58:41.143 に答える
-1

ヒントは次のとおりです。

int intValue = int.Parse("FFFFFF80", System.Globalization.NumberStyles.HexNumber);

見る:

C#は整数を16進数に変換し、再び元に戻します

于 2011-07-08T23:40:25.530 に答える