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

后端 未结 9 1301
名媛妹妹
名媛妹妹 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 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.

提交回复
热议问题