mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-22 11:46:34 +08:00

* SingleBitOperators * Tests * Update SingleBitOperatorTest.java * Update SingleBitOperators.java * Update SingleBitOperators.java * Update SingleBitOperators.java * Update SingleBitOperatorTest.java * deliting files * Update SingleBitOperators.java * Update SingleBitOperatorTest.java * Update SingleBitOperators.java * Update SingleBitOperators.java * Update SingleBitOperatorTest.java * Update SingleBitOperatorTest.java * Update and rename SingleBitOperators.java to SingleBitOperator.java * Update SingleBitOperatorTest.java * Update SingleBitOperator.java * Update SingleBitOperator.java * Update SingleBitOperator.java * style: declare private default constructor * fix: remove `SetBitTest.java` * Update and rename SingleBitOperator.java to SingleBitOperations.java * Update SingleBitOperatorTest.java * Update SingleBitOperations.java * Update and rename SingleBitOperatorTest.java to SingleBitOperationsTest.java --------- Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
35 lines
899 B
Java
35 lines
899 B
Java
package com.thealgorithms.bitmanipulation;
|
|
|
|
/*
|
|
* Author: lukasb1b (https://github.com/lukasb1b)
|
|
*/
|
|
|
|
public final class SingleBitOperations {
|
|
private SingleBitOperations() {
|
|
}
|
|
/**
|
|
* Flip the bit at position 'bit' in 'num'
|
|
*/
|
|
public static int flipBit(final int num, final int bit) {
|
|
return num ^ (1 << bit);
|
|
}
|
|
/**
|
|
* Set the bit at position 'bit' to 1 in the 'num' variable
|
|
*/
|
|
public static int setBit(final int num, final int bit) {
|
|
return num | (1 << bit);
|
|
}
|
|
/**
|
|
* Clears the bit located at 'bit' from 'num'
|
|
*/
|
|
public static int clearBit(final int num, final int bit) {
|
|
return num & ~(1 << bit);
|
|
}
|
|
/**
|
|
* Get the bit located at 'bit' from 'num'
|
|
*/
|
|
public static int getBit(final int num, final int bit) {
|
|
return ((num >> bit) & 1);
|
|
}
|
|
}
|