java - Divide byte into two smaller numbers -


like in topic. writing application must save ram possible. want split byte 2 parts 4 bits each (numbers 0 15) - how can save , later read values?

if want store 2 numbers (range 0-15) in 1 byte, here's example on how save , restore them. note have make sure original numbers within allowed range, otherwise not work.

    // store both numbers in 1 byte     byte firstnumber = 10;     byte secondnumber = 15;     final byte bothnumbers = (byte) ((firstnumber << 4) | secondnumber);      // retreive original numbers     firstnumber = (byte) ((bothnumbers >> 4) & (byte) 0x0f);     secondnumber = (byte) (bothnumbers & 0x0f); 

also worth noting if want save memory possible shouldn't using java start with. jvm consumes memory itself. native languages more suited requirement.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -