Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: what is Bitmasking?

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default what is Bitmasking?

    i currently had a glance with bit operators in java such as Summary of Operators (The Java™ Tutorials > Learning the Java Language > Language Basics) bitwise and bit shift operators. given the following code i understand how they work, and some articles telling the usage and difference of logical operators vs usage of bitwise operators(e.g && and & , || and |)

     
    System.out.println(3 & 6); // results in an integer 2
    System.out.println(3 | 6); // results in an integer 7
    System.out.println(3 ^ 6); // results in an integer 5
    i understand that the single &(bitwise AND), |(bitwise exclusive OR) and ^(bitwise inclusive OR) between 2 integers performs a Binary/Boolean multiplication and addition(given the logic of boolean arithmetic).



    System.out.println(16 << 3); // resutls in an integer 128
    this one shifts the binary value of 16, 3 times to the left, getting a value 128, vice versa if shifted using >> right shift operator, 16's binary value will be shifted to the right 3 times.


    The double sign logical operators performs a short circuit operation, but a single sign logical operators(bitwise) always evaluate all the operands(e.g false & true, will be both evaluated). i found out that, although Bitwise operators can be used in a Boolean EXPRESSION/CONDITION, it was not intended for it, my conclusion was, it was intended for a Boolean/Binary arithmetic. is my conclusion right?


    My question is, what is Bitmasking? is it a very technical hardcore thing to understand? articles i've read, doesnt give me a very handy answer.. maybe asking here might enlighten me given the knowledge i managed to learn with these operators..

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: what is Bitmasking?

    Do you have an example definition of "bitmasking" that you don't understand that you could post here?
    I've used the term to define a variable with some selected bits set on to be used to test the contents of another variable.
    The "bit mask" variable is a template that is used to see if the selected bits in the tested variable are set on or not. Simple example. I want to see if a number is odd. The mask would be: 00001. If that mask is ANDed with a numeric variable and the results are not zero, then that variable has the tested bit on and the number is odd.

  3. The Following User Says Thank You to Norm For This Useful Post:

    chronoz13 (September 29th, 2011)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: what is Bitmasking?

    this article masking : Java Glossary a java glossary defines some meaning and examples for what is masking is about using the bit operators. its a bit complicated in my standpoint right now, so thanks for giving a basic sample of the term bitmasking, so it can be used for an ODD / EVEN number checking program, other than a modular arithmetic.

  5. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: what is Bitmasking?

    i got the ODD/EVEN thanks norm
    Last edited by chronoz13; September 29th, 2011 at 08:01 AM.

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: what is Bitmasking?

    1 & 7 = 1 NOT 0
    Not 0 means the number is odd

    What are you expecting?

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: what is Bitmasking?

    Other way around:

    if the LSB (least significant bit, or bit furthest to the right) is set, a binary number is odd. Otherwise it's even.

    System.out.println((7 & 0x1) == 0); // true if 7 is even

    Also, it's much easier to use binary and hexadecimal numbers vs. decimal numbers when dealing with bit masking/shifting.

    edit: haha, I let convention and early mornings slip my mind over reality. It doesn't matter which operand is the mask and which is the value. Though everything else above is true.
    Last edited by helloworld922; September 29th, 2011 at 08:16 AM.

  8. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (September 29th, 2011)

  9. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: what is Bitmasking?

    1 & 7 = 1 NOT 0
    Not 0 means the number is odd

    What are you expecting?
    yes, sorry i was not paying enough attention on my code,

  10. #8
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: what is Bitmasking?

    if the LSB (least significant bit, or bit furthest to the right) is set, a binary number is odd. Otherwise it's even
    Is it 'use implementation detail week'?

    Checking the LSB for negative numbers relies on ints being represented that way. An even number is a number for which there's no remainder after division by 2. There are good uses of bitmasking, but the odd/even test is not one of them:

    c - How do I check if an integer is even or odd? - Stack Overflow

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: what is Bitmasking?

    That was the first one that came to mind as an easily understood concept to easily demonstrate.

  12. #10
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: what is Bitmasking?

    Then again, when I think of all the things for which I use bitmasking none of them would make a good example and many also rely on implementation details. Maybe I should pick nits out of someone else's thread. Carry on.

  13. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: what is Bitmasking?

    Right.
    I spent 10+ years as an assembler programmer. Did a lot of ANDing and ORing and shifting.