C# Language: Changing the First Four Bits in a Byte

后端 未结 9 1323
名媛妹妹
名媛妹妹 2021-02-14 07:01

In order to utilize a byte to its fullest potential, I\'m attempting to store two unique values into a byte: one in the first four bits and another in the second four bits. How

9条回答
  •  难免孤独
    2021-02-14 08:07

    I know you asked specifically about clearing out the first four bits, which has been answered several times, but I wanted to point out that if you have two values <= decimal 15, you can combine them into 8 bits simply with this:

        public int setBits(int upperFour, int lowerFour)
        {
            return upperFour << 4 | lowerFour;            
        }
    

    The result will be xxxxyyyy where

    xxxx = upperFour
    yyyy = lowerFour
    

    And that is what you seem to be trying to do.

提交回复
热议问题