Code Monkey home page Code Monkey logo

bit-lib4j's Introduction

Bit-lib4j Bit-lib4j CI Coverage Status Maven Central

Bit-Lib4j is an useful library to handle bytes or bits in Java.
With this library you can read/write data in a byte array with a custom size for Java primitive types.

Simple API

It is very easy to get started with bit-lib4j:

  • Read data from byte array
	byte[] array = new byte[]{0x12,0x25}
	BitUtils bit = new BitUtils(array);
	int res = bit.getNextInteger(4);        // read the first 4 bits to an integer
  • Create byte array with bit
	BitUtils bit = new BitUtils(8);
	bit.setNextInteger(3,3);        // set an integer on 3 bits
	bit.setNextInteger(1,5);        // set one value on 5 bits

	// Result
	bit.getData();                  // return Ox61  (0110 0001b)
  • Read/write signed values
	BitUtils bit = new BitUtils(4);
	bit.setNextInteger(-2 , 4);	    // set an integer (-2) on 4 bits

	// Result
	bit.getNextIntegerSigned(4);    // return -2

You can also use getNextSignedLong() to handle long signed values.

Handle bytes more easily

The class ByteUtils provided static methods to convert byte array to String, String to byte array, int to byte array, byte array to binary representation.

More documentation into the wiki

Download

Maven

<dependency>
  <groupId>com.github.devnied</groupId>
  <artifactId>bit-lib4j</artifactId>
  <version>1.5.2</version>
</dependency>

JAR

You can download this library on Maven central or in Github release tab

Dependencies

If you are not using Maven or some other dependency management tool that can understand Maven repositories, the list below is what you need to run bit-lib4j.

Runtime Dependencies

  • slf4j-api 1.7.30

Bugs

Please report bugs and feature requests to the GitHub issue tracker.
Forks and Pull Requests are also welcome.

Author

Millau Julien

Copyright and license

Copyright 2020 Millau Julien.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:

http://www.apache.org/licenses/LICENSE-2.0

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.

Analytics

bit-lib4j's People

Contributors

anthonygraignic avatar dependabot[bot] avatar devnied avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

bit-lib4j's Issues

Negative integer/long support

I'm tyring to use nagative values with no luck

byte[] array = new byte[1];
BitUtils bit = new BitUtils(array);
bit.setNextInteger(-2, 8);
byte[] data = bit.getData();
BitUtils bit2 = new BitUtils((data));
int nextInteger = bit2.getNextInteger(8)

nextInteger will 254, it seems that the getter doesnt use the sign bit.
Or maybe i'm doing something wrong?

Wrong max length in setNextInteger

Hi,
In BitUtils, the method setNextInteger only accepts a pLength <= 31, or an Integer can be 32 bits according to Integer javadoc.
So an IllegalArgumentException prevents to set an Integer of 32 bits length which is quite problematic

Constructor method why so slowly

	/**
	 * Constructor for empty byte tab
	 * 
	 * @param pSize
	 *            the size of the tab in bit
	 */
	public BitUtils(final int pSize) {
		byteTab = new byte[(int) Math.ceil(pSize / BYTE_SIZE_F)];
		size = pSize;
	}
System.out.println(pSize);

        Long startTime = System.currentTimeMillis();
        System.out.println("---" + (System.currentTimeMillis() - startTime));

        byte[] byteTab = new byte[(int) Math.ceil(pSize / BYTE_SIZE_F)];

        System.out.println("---" + (System.currentTimeMillis() - startTime));
        BitUtils bits = new BitUtils(pSize);

        System.out.println("---" + (System.currentTimeMillis() - startTime));

        //[version 16bit]
        bits.setNextInteger(bootLength, 16);
        System.out.println("---" + (System.currentTimeMillis() - startTime));

        // ่กŒ 8bit
        bits.setNextInteger(6, 8);
        System.out.println("---" + (System.currentTimeMillis() - startTime));
574
---0
---13
---5438
---5439
---5439

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.