Reading top nibble and bottom nibble in a byte

后端 未结 2 1516
野性不改
野性不改 2021-01-23 08:28

What\'s the correct way to handle two distinct values being stored in one byte of data. I have a byte that contains two nibbles each containing their own data. I want to read th

相关标签:
2条回答
  • 2021-01-23 08:35

    Use ANDoperators for both and shift the top nibble four bits to the right.

    $brake = $value & 0x0F;
    $throttle = ($value & 0xF0) >> 4;
    
    0 讨论(0)
  • 2021-01-23 08:53

    Check out the & operator, which is a bitwise AND. To get the first (least significant bit), do this:

    $lsb = $bits & 1;
    

    So, to get the whole "nibble":

    $break = $bits & 15;
    
    0 讨论(0)
提交回复
热议问题