10 Replies Latest reply: Aug 7, 2008 2:35 PM by 807589 RSS

    Least Significant Bit removal

    807589
      How do you remove the last k least significant bits in a int? The only way I know is something like
      int i;
      i >> k;
      i << k;
      Is there another bit operator or method for what I want?
        • 1. Re: Least Significant Bit removal
          807589
          How do you remove the last k least significant bits in a int?
          What do you mean by "remove"?

          ~
          • 2. Re: Least Significant Bit removal
            807589
            I guess I should of been more clear. I mean change the bits to '0'. Also just a curious question, how come
            int i;
            i >> 32
            does not do anything? Shouldn't it shift all 32 bits out and give you 0?
            • 3. Re: Least Significant Bit removal
              807589
              i &= ~(1<<(k-1))
              is equivalent to your example.
              • 4. Re: Least Significant Bit removal
                807589
                SarcasmMonster wrote:
                I guess I should of been more clear. I mean change the bits to '0'.
                Mask the bits out; i.e., AND them with zero.
                Also just a curious question, how come
                int i;
                i >> 32
                does not do anything? Shouldn't it shift all 32 bits out and give you 0?
                "If the promoted type of the left-hand operand is int, *only the five lowest-order bits of the right-hand operand are used as the shift distance.* It is as if the right-hand operand were subjected to a bitwise logical AND operator & (�15.22.1) with the mask value 0x1f. *The shift distance actually used is therefore always in the range 0 to 31, inclusive.*

                If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (�15.22.1) with the mask value 0x3f. The shift distance actually used is therefore always in the range 0 to 63, inclusive."

                [http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.19]

                ~
                • 5. Re: Least Significant Bit removal
                  807589
                  Bah, yawmark got here first, but I didn't do all this work for nothing lol. Here you go

                  Mask the bits you want to remove with 0's and the bits you want to keep with 1's and & them together like so.
                          int binaryToInt = Integer.parseInt("01010101", 2);
                          System.out.println( binaryToInt );
                          int maskLowerBytes = Integer.parseInt("11110000", 2);
                          int maskLowerBytesHexLiteral = 0xF0;
                  
                          int maskedWithZerosBytesRemoved = binaryToInt & maskLowerBytes;
                          System.out.println( maskedWithZerosBytesRemoved );
                          maskedWithZerosBytesRemoved = binaryToInt & maskLowerBytesHexLiteral;
                          System.out.println( maskedWithZerosBytesRemoved );
                  Of course using the hex literal is easier to read and quicker, I just used the binary conversion for demonstration purposes.
                  • 6. Re: Least Significant Bit removal
                    796440
                    Brushfire wrote:
                    int maskLowerBytesHexLiteral = 0xF0;
                    Um, no.

                    0xF0 is 240. You need to sign extend it. If you want to mask off the lower bits, your mask will be a negative number.

                    For instance, -1 (0xFFFFFFFF) with the last 5 bits masked off will be -32 (0xFFFFFFE0)

                    -1 & 0xF0 gives 240

                    -1 & -32 gives -32, which is what we want

                    Edited by: jverd on Aug 7, 2008 1:06 PM
                    • 7. Re: Least Significant Bit removal
                      807589
                      Just for the OPs benefit what he means is I should've masked like this 0xFFFFFFF0. Just in case my input was bigger than 8-bits which in this case it wasn't.
                      • 8. Re: Least Significant Bit removal
                        807589
                        BTW I probably should've done a 32- bit binary to int conversion, but I didn't feel like writing all them bloody 1's and 0's:)
                        • 9. Re: Least Significant Bit removal
                          796440
                          Brushfire wrote:
                          Just for the OPs benefit what he means is I should've masked like this 0xFFFFFFF0. Just in case my input was bigger than 8-bits which in this case it wasn't.
                          Yeah, sorry. Wasn't clear. Was trying to edit my message, but it was too late.
                          Brushfire wrote:
                          int maskLowerBytesHexLiteral = 0xF0;
                          What I meant to say was..

                          For instance, -1 (0xFFFFFFFF) with the last 5 bits masked off will be -32 (0xFFFFFFE0)

                          -1 & 0xE0 (224) gives 224

                          -1 & 0xFFE0 (65504) gives 65504

                          -1 & 0xFFFFFFE0 (-32) gives -32, which is what we want
                          • 10. Re: Least Significant Bit removal
                            807589
                            Yes mine was a little out of context as far as the entire int. What I was trying to show was the individual bits being masked, and the equivalent hex literal that makes typing oh so much easier (not to mention readability).