com.jamesratzlaff:bit-manip

Library for bitwise manipulations in arrays

License

License

GroupId

GroupId

com.jamesratzlaff
ArtifactId

ArtifactId

bit-manip
Last Version

Last Version

2019.04.03
Release Date

Release Date

Type

Type

jar
Description

Description

com.jamesratzlaff:bit-manip
Library for bitwise manipulations in arrays
Project URL

Project URL

https://github.com/jamesratzlaff/bit-manip
Source Code Management

Source Code Management

http://github.com/jamesratzlaff/bit-manip/tree/master

Download bit-manip

How to add to project

<!-- https://jarcasting.com/artifacts/com.jamesratzlaff/bit-manip/ -->
<dependency>
    <groupId>com.jamesratzlaff</groupId>
    <artifactId>bit-manip</artifactId>
    <version>2019.04.03</version>
</dependency>
// https://jarcasting.com/artifacts/com.jamesratzlaff/bit-manip/
implementation 'com.jamesratzlaff:bit-manip:2019.04.03'
// https://jarcasting.com/artifacts/com.jamesratzlaff/bit-manip/
implementation ("com.jamesratzlaff:bit-manip:2019.04.03")
'com.jamesratzlaff:bit-manip:jar:2019.04.03'
<dependency org="com.jamesratzlaff" name="bit-manip" rev="2019.04.03">
  <artifact name="bit-manip" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.jamesratzlaff', module='bit-manip', version='2019.04.03')
)
libraryDependencies += "com.jamesratzlaff" % "bit-manip" % "2019.04.03"
[com.jamesratzlaff/bit-manip "2019.04.03"]

Dependencies

compile (1)

Group / Artifact Type Version
com.jamesratzlaff : byte-functions jar 2019.04.01

Project Modules

There are no modules declared in this project.

bit-manip

Library for bitwise manipulations over arrays

Examples

Cyclic Shifter

//Given an array or (buffer) of bytes:
byte[] bytes =new byte[]{0b10000001,0b00011000,0b00000111};//100000010001100000000111
//Then invoking a bitwise cyclic right shift 2
Bitwise.CyclicShifter.Using.aByteArray(bytes,2);
//the contents of bytes is now 
//{0b11100000,0b010001100,0b0000000}                         111000000100011000000001
//Invoking a bitwise cyclic left shift 2
Bitwise.CyclicShifter.Using.aByteArray(bytes,-2);
//bytes is back to {0b10000001,0b00011000,0b0000111};        100000010001100000000111
//Then invoking a bitwise cyclic right shift 9
Bitwise.CyclicShifter.Using.aByteArray(bytes,9);
//the contents of bytes will be 
//{0b00000011,0b11000000,0b10001100}                         000000111100000010001100

There is also the method Bitwise.CyclicShifter.Using.aByteBuffer implemented for you as well as a generic constructor new CyclicShifter<T>(ToIntFunction<T> lengthGetter, ObjIntToByteFunction<T> getAtIndex, ObjIntByteConsumer<T> setAtIndex) to easily use whatever kind of byte container you please.

At the core all of the shifting and carrying is done using bitwise operators, therefore it runs pretty darn fast. It could be faster using concrete object interaction instead of method references.

Boundless buffer operations

Another object you may find useful is a CyclicByteBuffer which can be used to do operations over arrays smaller or larger than itself. A use of this object can be found in the Bitwise.Util.doOperation(byte[],IntBinaryOperator,byte...) method.

byte[] bytes =new byte[]{0b10000001,0b00011000,0b00000111};//100000010001100000000111
//Let's XOR these bytes with {0b11111111,0b00000000}
Util.doOperation(bytes, (a, b)->(a^b),(byte)-1,(byte)0);  
//the contents of bytes will now be
//{0b01111110,0b00011000,0b11111000}                         011111100001100011111000

Versions

Version
2019.04.03