How To Mask Out Lower 'x' Bits of Python Integer Of Unknown Size -
i looking way mask out (set ‘0’) lowest 4 bits of integer in python when don’t know total length of number. if knew length of integer beforehand set rest of mask 0xff, instance if integer 32 bits long use:
number &= 0xfffffff0
but if tried utilize 64-bit number there mask out bits 32-63. 2 things thought of create mask long (e.g. 0xffffffffffffffffff0) or take number of bits parameter function. both work plenty seem kludgy. there better/more pythonic way this?
number &= ~0xf
~
gives binary inversion of operand, ~0xf
equivalent 0x...fff0
integer size.
python python-2.7 bit-manipulation bitmask
No comments:
Post a Comment