Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Bit shifting an all that...

  • 22-11-2010 2:18pm
    #1
    Closed Accounts Posts: 6,408 ✭✭✭


    People! Help...

    [HTML]public static final int BITS = 8;
    public static final int ENTRIES = 1<<(BITS-1);
    public static final int MASK = ENTRIES-1;
    [/HTML]

    It's not HTML it's Java...

    Could anyone point me in the direction of how to figure out what's going here.
    What are the values of ENTRIES and MASK???

    Ok MASK is one less but what's it used for? What exactly is bitmasking?

    Any links to a decent explanation would be a great help...


Comments

  • Registered Users, Registered Users 2 Posts: 5,401 ✭✭✭DublinDilbert


    studiorat wrote: »
    People! Help...

    [HTML]public static final int BITS = 8;
    public static final int ENTRIES = 1<<(BITS-1);
    public static final int MASK = ENTRIES-1;
    [/HTML]

    It's not HTML it's Java...

    Could anyone point me in the direction of how to figure out what's going here.
    What are the values of ENTRIES and MASK???

    Ok MASK is one less but what's it used for? What exactly is bitmasking?

    Any links to a decent explanation would be a great help...


    The code is basically generating a MASK with the lower 7 bits set to 1.
    ENTRIES = 1<<(BITS-1)
    ENTRIES = 1<<(8-1)
    ENTRIES = 1<<(7) // left shift the value 1 seven places
    ENTRIES = 128 (or 0x80)

    MASK = ENTRIES-1;
    MASK = 0x80-1;
    MASK = 127( or 0x7F);


    The key is in the left shiftting. Each shift to the left is the equivelent of multipling by 2.






    public static final int BITS = 8;
    public static final int ENTRIES = 1<<(BITS-1);
    public static final int MASK = ENTRIES-1;
    is equivlent to this:-
    public static final int BITS = 8;
    public static final int ENTRIES = 0x80;
    public static final int MASK = 0x80-1 = 0x7f ;


  • Registered Users, Registered Users 2 Posts: 3,323 ✭✭✭padraig_f


    masking is where you're only interested in a subset of the bits. e.g. you have an 8-bit number, you want to examine the bottom two bits. you AND the number with a mask of 00000011 (binary):

    e.g.
    num = num & 0x3; // 0x3 = 00000011 binary

    which filters 'num', only including the bottom two bits.



    what he's doing in the example code is creating a mask to filter out anything above the bottom 7 bits.

    takes the number 1 and shifts it 7 bits to the left:
    public static final int BITS = 8;	
    public static final int ENTRIES = 1<<(BITS-1);
    

    ENTRIES = 10000000 (binary)

    subtracting 1 from this gives a 7 bit mask:

    01111111

    which typically will be AND'ed with another number to filter the bits, including only the bottom 7.


  • Closed Accounts Posts: 6,408 ✭✭✭studiorat


    padraig_f wrote: »

    which typically will be AND'ed with another number to filter the bits, including only the bottom 7.

    That's what I believe it does odd enough : I can post a link to the code.

    I was kind of hoping that shifting << 7 would give a value of 256 (the size of an audio buffer) which is what I think the developer said it was.

    Is there some convention I'm missing here counting a zero or something? I'm just a hacker here, but.. Why are we dealing with 7 bits and not 8?

    My head hurts, the masking and all that stuff is for the index of an array and not the values inside it. So I'm freaking out about the resolution of the data, but we're not masking the data we are the way the array is read. Does that make sense? or is it just pants?


  • Registered Users, Registered Users 2 Posts: 3,323 ✭✭✭padraig_f


    studiorat wrote: »
    Is there some convention I'm missing here counting a zero or something? I'm just a hacker here, but.. Why are we dealing with 7 bits and not 8?

    In a signed integer, the top bit is the sign-bit, so he could be filtering that out.

    Another possibility is it's a bug and he meant to filter on 8. It does seem curious that he said
    public static final int BITS = 8; 
    

    and created a mask for only 7 bits.


Advertisement