Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 16 bit alignment

I'm currently dealing with some integer values that represent offsets within a file, the numbers I have need to be aligned on 16-bit boundaries, however I'm a little unsure how to do this.

For example:

First number: 89023
16-bit aligned: 89024

Second number: 180725
16-bit aligned: 180736

Third number: 263824
Already 16-bit aligned, don't need to change it.

This is probably my maths failing me more than anything, but if anyone could advise on how to achieve this in Java, I'd appreciate it.

Thanks!

Update

I think I've just solved it, it's just a matter of modding the value with 16, then working out what's missing from 16.

So for example:

180725 % 16 = 5
16 - 5 = 11
180725 aligned to 16-bits is: 180736

Can someone just confirm that I'm doing that correctly?

like image 688
Tony Avatar asked Jan 27 '26 05:01

Tony


1 Answers

Yes that will work. The 16 bit boundary alignment just assures it will be on a multiple of 16. What you are doing there is ensuring that the value hits at the next value of 16, rounded up.

// find how far off of alignment you are, 0-15
offset = num % 16 
// determine how much further you need to move the value to achieve 16 bit alignment
// only use if offset > 0, otherwise you are already good
val = 16 - offset
like image 79
greedybuddha Avatar answered Jan 28 '26 17:01

greedybuddha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!