1. ## Help with bitmask math in dec

OK i just got out of bed with a huge headache so i can't seem to figure this out.
I need to create two bitmax for X amount of bits
first one is all "1"
Second one is every other bit has to be "1" and the others "0" but it does not matter if its 10101010... or 01010101...

First one was pretty easy. (x*x)-1
X=4
4*4 = 16
16 -1= 15
15 = binary 1111 ( 4 bits of "1"s)

I can't figure out how to do the other ones easily (low CPU usage)  Reply With Quote

2. Originally Posted by SvenBent OK i just got out of bed with a huge headache so i can't seem to figure this out.
I need to create two bitmax for X amount of bits
first one is all "1"
Second one is every other bit has to be "1" and the others "0" but it does not matter if its 10101010... or 01010101...

First one was pretty easy. (x*x)-1
X=4
4*4 = 16
16 -1= 15
15 = binary 1111 ( 4 bits of "1"s)

I can't figure out how to do the other ones easily (low CPU usage)
I don't think your first one is right. (5*5)-1 = 24, which is 110000 in binary. I use (1<<x)-1.
For the second one, I would probably just take the maximum length mask (such as 0xAAAA or 0x55555555) and mask it with (1<<x)-1 if needed.

One word of caution. (1<<x)-1 sometimes isn't good for creating a mask that is the size of the register since the shift can overflow on some processors. If that's a problem and all masks are at least one bit long, the mask can be, for example for a 32 bit register, 0xFFFFFFFF >> (32 - x).  Reply With Quote

3. One word of caution. (1<<x)-1 sometimes isn't good for creating a mask that is the size of the register since the shift can overflow on some processors. If that's a problem and all masks are at least one bit long, the mask can be 0xFFFFFFFF >> (32 - x).
IIRC, on Intel processors, if you do shift on 2^X bits long register (eg 32 bits register = 2^5 bits) by Y bits, then CPU only uses lowest X bits of Y.

Ie if R is 32-bits long then X is 5 (because 2^5 = 32) and R << 32 + Z == R << Z, so R << 32 == R. That's common pitfall on x86 CPUs. Maybe it works the same on (some) other architectures.  Reply With Quote

4. Originally Posted by Piotr Tarsa IIRC, on Intel processors, if you do shift on 2^X bits long register (eg 32 bits register = 2^5 bits) by Y bits, then CPU only uses lowest X bits of Y.

Ie if R is 32-bits long then X is 5 (because 2^5 = 32) and R << 32 + Z == R << Z, so R << 32 == R. That's common pitfall on x86 CPUs. Maybe it works the same on (some) other architectures.
ARM is better on shifting. If the operand comes from a register, it uses the low byte as the shift value.  Reply With Quote

5. First one is (1u<<x)-1; Second one is (1u<<(2*x))/3 or (2u<<(2*x))/3; The compiler should optimize out the division.  Reply With Quote

6. Thank you all. for the help i dont have bit manipulations tools in this "language" so everything i do has to be in "common" math
I did make a huge msitake on my first one i flipped 2^x into x^2 which becomes x^x so my bad.

all 1's = (2^x)-1
checkerboards are as matt said = ((2^x)-1)/3 or ((2^x)-1)2/3. which i should have know as its the same math properties as mipmaping textures  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•