简体   繁体   English

用按位运算符检查字节

[英]Checking byte with bitwise operators

I could use this: 我可以用这个:

unsigned long alpha = 140 | 130 << 8 | 255 << 16;

to set 140 to the first byte of alpha , 130 to the second and 255 as 3rd. 140设置为alpha的第一个字节,将130设置为第二个,并将255为3rd。 How do I do the opposite (ie checking a specific byte of alpha ) ? 我该如何做相反的操作(即检查alpha的特定字节)?


alpha & 255 // works for the first byte
alpha >> 16; // works for the 3rd byte

Shift the value x bits to the right and then use AND to restrict the number of bits you use. 将值x右移,然后使用AND限制使用的位数。 ie: (n >> 8) & 0xff or (n >> 16) & 0xff . 即: (n >> 8) & 0xff(n >> 16) & 0xff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM