Skip to content

Commit

Permalink
Merge branch 'publish-github'
Browse files Browse the repository at this point in the history
  • Loading branch information
gongxuanzhang committed Dec 12, 2023
2 parents 1cd2ac4 + 4318a98 commit 7055e23
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 70 deletions.
4 changes: 2 additions & 2 deletions easyByte-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.gongxuanzhang</groupId>
<groupId>io.github.gongxuanzhang</groupId>
<artifactId>easy-byte</artifactId>
<version>0.0.1</version>
<version>0.0.2-beta</version>
</parent>

<artifactId>easyByte-core</artifactId>
Expand Down
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));
}
}
}
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));
}
}
13 changes: 3 additions & 10 deletions easyByte-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,16 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.gongxuanzhang</groupId>
<groupId>io.github.gongxuanzhang</groupId>
<artifactId>easy-byte</artifactId>
<version>0.0.1</version>
<version>0.0.2-beta</version>
</parent>

<groupId>com.zhonghaiwenda</groupId>
<artifactId>easyByte-example</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.gongxuanzhang</groupId>
<groupId>io.github.gongxuanzhang</groupId>
<artifactId>easyByte-core</artifactId>
<version>0.0.1</version>
</dependency>
Expand Down
Loading

0 comments on commit 7055e23

Please sign in to comment.