10 Replies Latest reply on Aug 7, 2008 8:36 PM by 807589

# Least Significant Bit removal

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
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
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
``i &= ~(1<<(k-1))``
• ###### 4. Re: Least Significant Bit removal
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
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 );

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
Brushfire wrote:
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
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
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
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: