If I wanted to extract 8 bits from a hexadecimal number and store it somewhere else, what's the most cleanest way to do this? I don't think I properly understand 开发者_如何学编程bit shifting. For example, I know that to extract a the single nth bit, it would simply be ( hex >> n ) & 1, and of course I could do this 8 times and connect them to together, but I guessing there's a much easier solution than that?
Edit: The hexadecimal is 4 bytes, and I need to be able to extract anyone of these 4 bytes.
Are you asking how to extract 8 bits from an integer?
That's just (number >> n) & 0xFF to get the n to n+15th bit
If you have a hexadecimal string, it's a bit trickier.
精彩评论