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

后端 未结 9 1697
南旧
南旧 2021-02-14 07:23

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 07:52

    I'm not really sure what your method there is supposed to do, but here are some methods for you:

    void setHigh(ref byte b, byte val) {
        b = (b & 0xf) | (val << 4);
    }
    
    byte high(byte b) {
        return (b & 0xf0) >> 4;
    }
    
    void setLow(ref byte b, byte val) {
        b = (b & 0xf0) | val;
    }
    
    byte low(byte b) {
        return b & 0xf;
    }
    

    Should be self-explanatory.

提交回复
热议问题