开发者

Python bitwise operations confusion

开发者 https://www.devze.com 2022-12-09 13:07 出处:网络
I came up with this \"magic string\" to meet the ID3 tagging specification: The ID3v2 tag size is encoded with four bytes where the most significant bit (bit 7) is set to zero in every byte, making

I came up with this "magic string" to meet the ID3 tagging specification:

The ID3v2 tag size is encoded with four bytes where the most significant bit (bit 7) is set to zero in every byte, making a total of 28 bits. The zeroed bits are ignored, so a 257 bytes long tag is represented as $00 00 02 01.

>>> hex_val = 0xFFFFFFFF
>>> str.format('0b{0:07b}{1:07b}{2:07b}{3:07b}', ((hex_val >> 24) & 0xEF),
                                                 ((hex_val >> 16) & 0xEF), 
                                                 ((hex_val >>  8) & 0xEF),
                                                 ((hex_val >>  0) & 0xEF))
'0b11101111111011111110111111101111'

Why does it not equal:

'0b11111111111111111111111111111111'
开发者_如何学编程

?

If anyone cares, this seems to work:

>>> int(str.format('0b{0:07b}{1:07b}{2:07b}{3:07b}', ((hex_val >> 24) & 0xFE),
                                                     ((hex_val >> 16) & 0xFE),
                                                     ((hex_val >>  8) & 0xFE), 
                                                     ((hex_val >>  0) & 0xFE)), 2)


I think you are confusing the and and the or operations.

  • bitwise and: return a number with only bits that are in both operands set.
  • bitwise or: return a number with bits that are in either of the operands set.


Sorry getting my 7s and Es confused

Corrected code:

>>> str.format('0b{0:07b}{1:07b}{2:07b}{3:07b}', ((hex_val >> 24) & 0x7F),
                                                 ((hex_val >> 16) & 0x7F),
                                                 ((hex_val >>  8) & 0x7F),
                                                 ((hex_val >>  0) & 0x7F))


It does not equal all ones because you're masking out the 4th bit using the & operator!

0

精彩评论

暂无评论...
验证码 换一张
取 消