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

后端 未结 9 1335
名媛妹妹
名媛妹妹 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:06

    Assuming newVal contains the value you want to store in origVal. Do this for the 4 least significant bits:

    byte origVal = ???;
    byte newVal = ???
    orig = (origVal & 0xF0) + newVal;
    

    and this for the 4 most significant bits:

    byte origVal = ???;
    byte newVal = ???
    orig = (origVal & 0xF) + (newVal << 4);
    

提交回复
热议问题