C#, bits & bytes - How do I retrieve bit values from a byte?

前端 未结 5 1033
陌清茗
陌清茗 2020-12-29 08:04

I\'m reading some values from a single byte. I\'m told in the user-manual that this one byte contains 3 different values. There\'s a table that looks like this:

5条回答
  •  情歌与酒
    2020-12-29 08:38

    1. Potayto, potahto.

    2. You'd use shifts and masks to flatten out the undesired bits, like such:

      byte b = something; // b is our byte
      
      int size = b & 0x7;
      int scale = (b >> 3) & 0x3;
      int position = (b >> 5) & 0x7;
      

提交回复
热议问题