Results 1 to 6 of 6

Thread: Help with bitmask math in dec

  1. #1
    Member
    Join Date
    Sep 2007
    Location
    Denmark
    Posts
    856
    Thanks
    45
    Thanked 104 Times in 82 Posts

    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)

  2. #2
    Member
    Join Date
    Jan 2014
    Location
    Bothell, Washington, USA
    Posts
    685
    Thanks
    153
    Thanked 177 Times in 105 Posts
    Quote Originally Posted by SvenBent View Post
    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).
    Last edited by Kennon Conrad; 26th April 2015 at 21:49.

  3. #3
    Member
    Join Date
    Jun 2009
    Location
    Kraków, Poland
    Posts
    1,471
    Thanks
    26
    Thanked 120 Times in 94 Posts
    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.

  4. #4
    Member
    Join Date
    Jan 2014
    Location
    Bothell, Washington, USA
    Posts
    685
    Thanks
    153
    Thanked 177 Times in 105 Posts
    Quote Originally Posted by Piotr Tarsa View Post
    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.

  5. #5
    Expert
    Matt Mahoney's Avatar
    Join Date
    May 2008
    Location
    Melbourne, Florida, USA
    Posts
    3,255
    Thanks
    306
    Thanked 778 Times in 485 Posts
    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.

  6. #6
    Member
    Join Date
    Sep 2007
    Location
    Denmark
    Posts
    856
    Thanks
    45
    Thanked 104 Times in 82 Posts
    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

Similar Threads

  1. Math help or special calculator needed
    By SvenBent in forum The Off-Topic Lounge
    Replies: 19
    Last Post: 13th September 2014, 21:43
  2. the latest rec math contest is just starting
    By willvarfar in forum The Off-Topic Lounge
    Replies: 0
    Last Post: 15th March 2010, 00:12

Posting Permissions

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