-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
290 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
easyByte-core/src/main/java/org/gongxuanzhang/easybyte/core/tool/BitOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright 2023 sql-insight and the original author or authors <[email protected]>. | ||
* | ||
* Licensed under the GNU Affero General Public License v3.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://github.com/implement-study/sql-insight/blob/main/LICENSE | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.gongxuanzhang.easybyte.core.tool; | ||
|
||
/** | ||
* bit operator | ||
* | ||
* @author [email protected] | ||
**/ | ||
public class BitOperator { | ||
|
||
private BitOperator() { | ||
|
||
} | ||
|
||
|
||
/** | ||
* set a bit from byte to one | ||
* | ||
* @param origin the byte | ||
* @param n bit index, right end is 0 | ||
* @return number after update | ||
**/ | ||
public static byte setBitToOne(byte origin, int n) { | ||
checkSize(n, Byte.SIZE); | ||
byte mask = (byte) (1 << n); | ||
return (byte) (origin | mask); | ||
} | ||
|
||
|
||
/** | ||
* like {@link BitOperator#setBitToOne(byte, int)} | ||
**/ | ||
public static short setBitToOne(short origin, int n) { | ||
checkSize(n, Short.SIZE); | ||
short mask = (short) (1 << n); | ||
return (short) (origin | mask); | ||
} | ||
|
||
/** | ||
* like {@link BitOperator#setBitToOne(byte, int)} | ||
**/ | ||
public static int setBitToOne(int origin, int n) { | ||
checkSize(n, Integer.SIZE); | ||
int mask = 1 << n; | ||
return (origin | mask); | ||
} | ||
|
||
/** | ||
* like {@link BitOperator#setBitToOne(byte, int)} | ||
**/ | ||
public static long setBitToOne(long origin, int n) { | ||
checkSize(n, Long.SIZE); | ||
long mask = 1L << n; | ||
return origin | mask; | ||
} | ||
|
||
|
||
/** | ||
* set a bit from byte to zero | ||
* | ||
* @param origin the byte | ||
* @param n bit index, right end is 0 | ||
* @return number after update | ||
**/ | ||
public static byte setBitToZero(byte origin, int n) { | ||
checkSize(n, Byte.SIZE); | ||
byte mask = (byte) ~(1 << n); | ||
return (byte) (origin & mask); | ||
} | ||
|
||
|
||
/** | ||
* like {@link BitOperator#setBitToZero(byte, int)} | ||
**/ | ||
public static short setBitToZero(short origin, int n) { | ||
checkSize(n, Short.SIZE); | ||
short mask = (short) ~(1 << n); | ||
return (short) (origin & mask); | ||
} | ||
|
||
/** | ||
* like {@link BitOperator#setBitToZero(byte, int)} | ||
**/ | ||
public static int setBitToZero(int origin, int n) { | ||
checkSize(n, Integer.SIZE); | ||
int mask = ~(1 << n); | ||
return (origin & mask); | ||
} | ||
|
||
/** | ||
* like {@link BitOperator#setBitToZero(byte, int)} | ||
**/ | ||
public static long setBitToZero(long origin, int n) { | ||
checkSize(n, Long.SIZE); | ||
long mask = ~(1L << n); | ||
return origin & mask; | ||
} | ||
|
||
|
||
private static void checkSize(int n, int checkSize) { | ||
if (n < 0 || n >= checkSize) { | ||
throw new IllegalArgumentException("length must between 0 and " + (checkSize - 1)); | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
easyByte-core/src/test/java/org/gongxuanzhang/easybyte/core/tool/BitOperatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package org.gongxuanzhang.easybyte.core.tool; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class BitOperatorTest { | ||
|
||
@Test | ||
void testSetBitToOneByte() { | ||
byte byteValue = 0b1010101; | ||
int bitIndex = 3; | ||
|
||
byte resultAfterSettingOne = BitOperator.setBitToOne(byteValue, bitIndex); | ||
assertEquals(0b1011101, resultAfterSettingOne); | ||
} | ||
|
||
@Test | ||
void testSetBitToZeroByte() { | ||
byte byteValue = 0b1011101; | ||
int bitIndex = 3; | ||
|
||
byte resultAfterSettingZero = BitOperator.setBitToZero(byteValue, bitIndex); | ||
assertEquals(0b1010101, resultAfterSettingZero); | ||
} | ||
|
||
@Test | ||
void testSetBitToOneShort() { | ||
short shortValue = (short) 0b0; | ||
int bitIndex = 7; | ||
|
||
short resultAfterSettingOne = BitOperator.setBitToOne(shortValue, bitIndex); | ||
assertEquals(0b10000000, resultAfterSettingOne); | ||
} | ||
|
||
@Test | ||
void testSetBitToZeroShort() { | ||
short shortValue = (short) 0b1010111010101010; | ||
int bitIndex = 10; | ||
|
||
short resultAfterSettingZero = BitOperator.setBitToZero(shortValue, bitIndex); | ||
assertEquals((short) 0b1010101010101010, resultAfterSettingZero); | ||
} | ||
|
||
@Test | ||
void testSetBitToOneInt() { | ||
int intValue = 0b10101010101010100010101010101010; | ||
int bitIndex = 15; | ||
|
||
int resultAfterSettingOne = BitOperator.setBitToOne(intValue, bitIndex); | ||
assertEquals(0b10101010101010101010101010101010, resultAfterSettingOne); | ||
} | ||
|
||
@Test | ||
void testSetBitToZeroInt() { | ||
int intValue = 0b10101010101110101010101010101010; | ||
int bitIndex = 20; | ||
|
||
int resultAfterSettingZero = BitOperator.setBitToZero(intValue, bitIndex); | ||
assertEquals(0b10101010101010101010101010101010, resultAfterSettingZero); | ||
} | ||
|
||
@Test | ||
void testSetBitToOneLong() { | ||
long longValue = 0xffffffffL; | ||
int bitIndex = 31; | ||
|
||
long resultAfterSettingOne = BitOperator.setBitToOne(longValue, bitIndex); | ||
assertEquals(0xffffffffL, resultAfterSettingOne); | ||
} | ||
|
||
@Test | ||
void testSetBitToZeroLong() { | ||
long longValue = 0xffffffffffffffffL; | ||
int bitIndex = 31; | ||
|
||
long resultAfterSettingZero = BitOperator.setBitToZero(longValue, bitIndex); | ||
assertEquals(0xffffffff7fffffffL, resultAfterSettingZero); | ||
} | ||
|
||
@Test | ||
void testInvalidBitIndexThrowsException() { | ||
byte byteValue = 0b1010101; | ||
int invalidBitIndex = 8; | ||
|
||
assertThrows(IllegalArgumentException.class, () -> BitOperator.setBitToOne(byteValue, invalidBitIndex)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.