PowerOf2Util { public static int align(final int value, final int pow2alignment) { return (value + (pow2alignment - 1)) & ~(pow2alignment - 1); } private  PowerOf2Util(); static int align(final int value, final int pow2alignment); static boolean isPowOf2(final int value); static boolean isAligned(final long value, final int pow2alignment);  }
@Test public void shouldAlignToNextMultipleOfAlignment() { final int alignment = 512; assertThat(align(0, alignment), is(0)); assertThat(align(1, alignment), is(alignment)); assertThat(align(alignment, alignment), is(alignment)); assertThat(align(alignment + 1, alignment), is(alignment * 2)); final int remainder = Integer.MAX_VALUE % alignment; final int alignedMax = Integer.MAX_VALUE - remainder; assertThat(align(alignedMax, alignment), is(alignedMax)); assertThat(align(Integer.MAX_VALUE, alignment), is(Integer.MIN_VALUE)); }