Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

bit hacks java

public class Sample {
	public static void main(String[] args) {
		int x = 16;
		// Turn off the rightmost 1-bit in x
		int y = x & (x-1);
		System.out.println("After turning off rightmost 1-bit: "  + y); // 0
		x = 48;	
		// Isolate rightmost 1-bit in x
		y = x & (-x);
		System.out.println("After isolating rightmost 1-bit: " + y); // 16
		// Turn on the rightmost 0-bit
		y = x | (x+1);
		System.out.println("After turning on rightmost 0-bit: " + y); // 49 
	}
}
 
PREVIOUS NEXT
Tagged: #bit #hacks #java
ADD COMMENT
Topic
Name
4+7 =