Mar 25, 2025

[Bits manipulation]

align:

return (value + align_size - 1) & (~(align_size - 1);

has_single_bit:

return x && !(x & (x - 1));

midpoint:

return (a & b) + (a ^ b) / 2;

負i保 減一去
Set union A | B 
Set intersection A & B 
Set subtraction A & ~B 
Set negation ALL_BITS ^ A or ~A 
Set bit A |= 1 << bit 
Clear bit A &= ~(1 << bit) 
Test bit (A & 1 << bit) != 0 
Extract last bit A & -A or A & ~(A - 1) or x ^ (x & (x - 1)) 
Remove last bit A & (A - 1) 
Get all 1-bits ~0

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.