public class BitOperators {
static public int set(int position, int number) {
return number | (1<<position);
}
static public int get(int position, int number) {
return (number >> position) & 1;
}
static public int flip(int position, int number) {
return (number ^ (1<<position));
}
static public int clear(int position, int number) {
return (number & ~(1<<position));
}
}
I've got a masters degree in computer science and over 10 years of experience building web-based systems using Java/J2EE, Ruby, Rails and PHP. I'm a strong believer in the effectiveness of Agile Methods. Read more »
Basic Bit Operations in Java
public class BitOperators { static public int set(int position, int number) { return number | (1<<position); } static public int get(int position, int number) { return (number >> position) & 1; } static public int flip(int position, int number) { return (number ^ (1<<position)); } static public int clear(int position, int number) { return (number & ~(1<<position)); } }