0

Like in topic. I am writing application where I must save as much RAM as possible. I want to split byte into two parts 4 bits each (numbers from 0 to 15) - how can I save and later read this values?

Petr
  • 62,528
  • 13
  • 153
  • 317
Mac70
  • 320
  • 5
  • 15
  • I don't think you can have a nibble in Java? But you might need bitwise &, | to set and/or read the bits – Lews Therin Jul 18 '13 at 10:53
  • Since numbers in Java are signed, when using 4 bits you will probably end up with numbers from `-8` to `7`, unless you do some maths yourself. – afsantos Jul 18 '13 at 10:58
  • 2
    Can you please explain how splitting a byte in 2 nibbles is going to help you save memory space? – m0skit0 Jul 18 '13 at 11:03
  • @VisruthCV convert to binary? All data is already in binary form when in a computer. What do you mean exactly? – m0skit0 Jul 18 '13 at 11:05
  • 1
    @afsantos No since there's no nibble data type. – m0skit0 Jul 18 '13 at 11:05
  • Do you mean to store 2 numbers in one byte? – m0skit0 Jul 18 '13 at 11:07
  • How exactly it is going to save any RAM? – anubhava Jul 18 '13 at 11:09
  • 1 byte is taking less space than 2 bytes - I need to store a lot of very small numbers and read them rarely. – Mac70 Jul 18 '13 at 11:13
  • 1
    @VisruthCV sorry that's pure nonsense. 12 is already stored as 1100 by the computer. A computer only knows binary. You are confusing the representation of a number with its value. – m0skit0 Jul 18 '13 at 11:14
  • @VisruthCV Sorry but a `boolean` in Java most likely does not take 1 bit. This JVM implementation-dependent but even if it sounds counter-intuitive using 1 bit might be slower than using a byte. This is due to the fact that smallest unit used by a CPU for a programmer is byte and not bit. Also an array is an object in Java so it will take even more space. – m0skit0 Jul 18 '13 at 11:19
  • 3
    Learn about shift and mask operations -- `<<, >>, &, |`. It's a little clumsy at times, but basically very simple to combine several n-bit values into a byte or int, and not particularly inefficient. – Hot Licks Jul 18 '13 at 11:23
  • 1
    Yeah, `boolean` values occupy at least a byte in most implementations. In some they may actually occupy a 4-byte word when in the stack or in an instance field, though generally only 8 bits in an array. For the JVM I worked on we considered doing 1-bit arrays, but it was too messy for too little benefit. – Hot Licks Jul 18 '13 at 11:27

2 Answers2

4

If what you want is store 2 numbers (range 0-15) in one byte, here's an example on how to save and restore them. Note that you have to make sure your original numbers are within the allowed range, otherwise this will not work.

    // Store both numbers in one byte
    byte firstNumber = 10;
    byte secondNumber = 15;
    final byte bothNumbers = (byte) ((firstNumber << 4) | secondNumber);

    // Retreive the original numbers
    firstNumber = (byte) ((bothNumbers >> 4) & (byte) 0x0F);
    secondNumber = (byte) (bothNumbers & 0x0F);

Also worth noting that if you want to save as much memory as possible you shouldn't be using Java to start with. JVM already consumes memory by itself. Native languages are much more suited to this requirement.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
3

You can get lower bits as

byte lower = b & 0xF; 

higher bits as

byte higher = (b >> 4) & 0xF; 

and back to one byte

byte b  = (byte) (lower + (higher << 4));
LaurentY
  • 7,495
  • 3
  • 37
  • 55
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275