Low Level Class Methods in Java

 

1. Bitwise and integer operations

Integer & Long

(Also in Long with long versions)

  • bitCount(int x) — popcount (Hamming weight)

  • numberOfLeadingZeros(int x)

  • numberOfTrailingZeros(int x)

  • highestOneBit(int x) — isolate MSB

  • lowestOneBit(int x) — isolate LSB

  • rotateLeft(int x, int n)

  • rotateRight(int x, int n)

  • reverse(int x) — bit reversal

  • reverseBytes(int x) — byte swap

  • compareUnsigned(int x, int y)

  • divideUnsigned(int dividend, int divisor)

  • remainderUnsigned(int dividend, int divisor)

  • toUnsignedLong(int x)

  • Const: BYTES, SIZE


2. Unsigned helpers

  • Byte.toUnsignedInt(byte b)

  • Short.toUnsignedInt(short s)

  • Integer.toUnsignedLong(int i)

  • Integer.compareUnsigned(...), Integer.divideUnsigned(...), Integer.remainderUnsigned(...)

  • Same for Long (compareUnsigned, divideUnsigned, remainderUnsigned)


3. IEEE-754 float/double bit manipulation

Float

  • floatToIntBits(float f) — normalizes NaN

  • floatToRawIntBits(float f) — preserves NaN payload

  • intBitsToFloat(int bits)

  • isFinite(float f)

  • isNaN(float f)

  • isInfinite(float f)

  • compare(float f1, float f2) — total order (with NaN handling)

  • sum(...), max(...), min(...) — exact for +0.0 / -0.0

Double

  • doubleToLongBits(double d)

  • doubleToRawLongBits(double d)

  • longBitsToDouble(long bits)

  • Same isFinite, isNaN, isInfinite, compare, sum, max, min


4. IEEE-754 Math helpers

Math / StrictMath:

  • nextUp, nextDown

  • ulp

  • scalb — fast x * 2^n

  • getExponent

  • copySign

  • fma — fused multiply-add

  • floorDiv, floorMod — correct signed division/mod

  • multiplyExact (int/long) — detect overflow

  • addExact, subtractExact, negateExact, incrementExact, decrementExact

  • multiplyHigh(long x, long y) — high 64 bits of 128-bit multiply (intrinsic on some CPUs)

  • floorDiv, floorMod (correct for negatives)

  • hypot, expm1, log1p — high precision variants

  • Math.clamp(...) — no built-in until Java 23 (but easy to implement)


5. Bulk memory & array ops

  • System.arraycopy(src, srcPos, dst, dstPos, length) — often intrinsic

  • Arrays.fill(array, value) — bulk fill

  • Arrays.mismatch(array1, array2) — first differing index (vectorized in some JDKs)

  • Arrays.equals(...) — often intrinsic for primitives

  • Arrays.hashCode(...) — optimized for primitives


6. Byte order, endian conversion

  • ByteOrder.nativeOrder()

  • ByteBuffer.order(ByteOrder)

  • Integer.reverseBytes(int), Long.reverseBytes(long), Short.reverseBytes(short)


7. Bit sets and big integers

BitSet

  • Logical ops: and, or, xor, andNot

  • Queries: nextSetBit, nextClearBit, previousSetBit, previousClearBit

  • Bulk: flip(from, to), set(from, to, value), clear(from, to)

BigInteger

  • setBit, clearBit, flipBit

  • testBit

  • bitCount, bitLength

  • Arbitrary shiftLeft/shiftRight

  • modInverse, modPow — useful in number theory


8. Concurrency / atomics / fences

java.util.concurrent.atomic

  • AtomicInteger, AtomicLong, AtomicReference, etc.:

    • getAndAdd, getAndSet, compareAndSet, weakCompareAndSet

  • AtomicIntegerArray, AtomicLongArray

  • AtomicIntegerFieldUpdater, AtomicLongFieldUpdater

VarHandle (Java 9+)

  • Fine-grained memory ordering:

    • getOpaque, setOpaque

    • getAcquire, setRelease

    • compareAndSet, compareAndExchange

    • getAndAdd, getAndBitwiseOr, etc.

  • Works for arrays, fields, and off-heap buffers

Thread

  • onSpinWait() — hint to CPU in spin loops (Java 9+)


9. Checksums and CRC

  • java.util.zip.CRC32 — hardware-accelerated on some CPUs

  • java.util.zip.Adler32 — simpler checksum

  • java.util.zip.Checksum — common interface


10. Misc numeric / low-level odds & ends

  • Short.reverseBytes(short)

  • Character.getNumericValue(char) — safe digit parsing

  • Character.digit(char, radix)

  • Locale.ROOT — avoids locale issues in numeric parsing

  • String.format(Locale.ROOT, ...) — deterministic formatting


11. Memory-mapped & off-heap access

  • FileChannel.map(MapMode.READ_WRITE, position, size)MappedByteBuffer

  • ByteBuffer.allocateDirect(size)

  • .asFloatBuffer(), .asIntBuffer(), .asLongBuffer() — typed views

  • MappedByteBuffer.force() — flush changes to disk

  • Java 19+: MemorySegment / MemoryLayout / VarHandle (Project Panama) for C-like struct access


12. Rare / underused intrinsics

These are mapped to CPU instructions on HotSpot when possible:

  • Math.multiplyHigh(long, long) — high 64 bits of a 128-bit multiply

  • Integer.bitCount, Long.bitCount — POPCNT

  • Integer.numberOfLeadingZeros, Long.numberOfLeadingZeros — CLZ

  • Integer.numberOfTrailingZeros, Long.numberOfTrailingZeros — CTZ

  • Integer.reverse, Long.reverse — bit reversal

  • System.arraycopy — memmove

  • Math.fma — FMA3/FMA4

  • Thread.onSpinWait — PAUSE instruction on x86


13. Constant values

  • Integer.BYTES, Integer.SIZE — number of bytes/bits in type

  • Same for Byte, Short, Long, Float, Double

  • Float.MIN_NORMAL — smallest normalized float

  • Float.MIN_VALUE — smallest positive subnormal

  • Float.MAX_EXPONENT, MIN_EXPONENT

  • Double.MIN_NORMAL, Double.MIN_VALUE, etc.

Comments

Popular posts from this blog

Neon Bulb Oscillators

23 Circuits you can Build in an Hour - Free Book

Q Multiplier Circuits