Skip to content

Instantly share code, notes, and snippets.

@jamezrin
Last active July 6, 2017 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamezrin/0def3e16407ffa7efca6b4eaaa31100a to your computer and use it in GitHub Desktop.
Save jamezrin/0def3e16407ffa7efca6b4eaaa31100a to your computer and use it in GitHub Desktop.
Java class to store boolean values on numbers
package com.jaimemartz.bitexample;
import static com.jaimemartz.bitexample.BitHelper.*;
public class BitExample {
/* Output:
Testing at bit: 1
Starting status: false
Setting to true: 100000000000001
Current status: true
Flipping bit: 100000000000000
Current status: false
Flipping bit: 100000000000001
Current status: true
Setting to false: 100000000000000
Current status: false
Testing at bit: 2
Starting status: false
Setting to true: 100000000000010
Current status: true
Flipping bit: 100000000000000
Current status: false
Flipping bit: 100000000000010
Current status: true
Setting to false: 100000000000000
Current status: false
*/
private static short number = 0b100000000000000;
public static void main(String[] args) {
test((byte) 1);
test((byte) 2);
}
private static void test(byte bit) {
System.out.println("Testing at bit: " + bit);
System.out.println("Starting status: " + get(number, bit) + "\n");
System.out.println("Setting to true: " + bin(number = (short) set(number, bit, true)));
System.out.println("Current status: " + get(number, bit) + "\n");
System.out.println("Flipping bit: " + bin(number = (short) flip(number, bit)));
System.out.println("Current status: " + get(number, bit) + "\n");
System.out.println("Flipping bit: " + bin(number = (short) flip(number, bit)));
System.out.println("Current status: " + get(number, bit) + "\n");
System.out.println("Setting to false: " + bin(number = (short) set(number, bit, false)));
System.out.println("Current status: " + get(number, bit) + "\n");
}
}
package com.jaimemartz.bitexample;
/**
* Little utility for storing booleans as bits in numbers
* @author mrjaime1999@gmail.com
*/
public class BitHelper {
/**
* @param lhs first side
* @param rhs second side
* @return result of the AND operation
*/
public static Number and(Number lhs, Number rhs) {
return lhs.longValue() & rhs.longValue();
}
/**
* @param lhs first side
* @param rhs second side
* @return result of the OR operation
*/
public static Number or(Number lhs, Number rhs) {
return lhs.longValue() | rhs.longValue();
}
/**
* @param lhs first side
* @param rhs second side
* @return result of the XOR operation
*/
public static Number xor(Number lhs, Number rhs) {
return lhs.longValue() ^ rhs.longValue();
}
/**
* @param lhs first side
* @param rhs second side
* @return result of the EQ operation
*/
public static boolean eq(Number lhs, Number rhs) {
return lhs.longValue() == rhs.longValue();
}
/**
* @param number the number as reference
* @return result of the NOT operation
*/
public static Number not(Number number) {
return ~number.longValue();
}
/**
* @param number the number as reference
* @param bitPos the position of the bit to read the value
* @return the boolean value at stored at bitPos
*/
public static boolean get(Number number, byte bitPos) {
Number mask = mask(bitPos);
Number res = and(number, mask);
return eq(res, mask);
}
/**
* @param number the number as reference
* @param bitPos the position of the bit to store the value
* @param value the boolean value
* @return the number after setting the value
*/
public static Number set(Number number, byte bitPos, boolean value) {
Number res = value ? or(number, mask(bitPos)) :
and(number, not(mask(bitPos)));
return safe(number.getClass(), res);
}
/**
* @param number the number as reference
* @param bitPos the position of the bit to flip
* @return number with after the bit flip
*/
public static Number flip(Number number, byte bitPos) {
Number res = xor(number, mask(bitPos));
return safe(number.getClass(), res);
}
/**
* @param clazz the resultant class to be converted to
* @param object the original number
* @return value of object with the type of clazz
*/
public static Number safe(Class<? extends Number> clazz, Number object) {
if (clazz.equals(Integer.class)) {
return object.intValue();
} else if(clazz.equals(Byte.class)) {
return object.byteValue();
} else if(clazz.equals(Long.class)) {
return object.longValue();
} else if(clazz.equals(Short.class)) {
return object.shortValue();
} else if(clazz.equals(Double.class)) {
return object.doubleValue();
} else if(clazz.equals(Float.class)) {
return object.floatValue();
} else throw new IllegalStateException();
}
/**
* @param bitPos the position (starting at 1)
* @return the mask of a bit at bitPos
*/
public static Number mask(byte bitPos) {
if (bitPos <= 0) {
throw new IllegalArgumentException("The parameter bitPos has to be greater than 1");
}
return 1 << bitPos - 1;
}
/**
* @param number the number of which we want the binary representation
* @return binary value of number
*/
public static String bin(Number number) {
return Long.toBinaryString(number.longValue());
}
/**
* @param number the number of which we want the hexadecimal representation
* @return hexadecimal value of number
*/
public static String hex(Number number) {
return "0x" + Long.toHexString(number.longValue());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment