4-bit integer number

Hey, new to the forums but trying to make my way with java and LWJGL!

Simple question, I need to minimise the space that my program uses by minimising unnecessary data. Eg. why use an integer when you can use a short, or a byte. But in this case, I would like to use a 4-bit integer data type. Is this possible?

Thankyou.

Yes it’s possible, but unless you’re programming some embedded device with hardly any memory then it very very highly unlikely to be worth the trouble. The other possibility is if you must move or visit massive amounts of memory regularly. So if it’s just because you want to “save memory” then don’t bother.

The one caveat to this particular rule is, you should use chars or shorts for index buffers (not ints), as current drivers and hardware can be mysteriously extra slow if you try and use ints.

Cas :slight_smile:

Thankyou for the information.

I think I might just stick with bytes!

What are you developing?

Just to toss more wood on the fire: Bit-packing can be very efficient in terms of speed. But the extra work generated is generally not worth the effort. There are other situations where it can be useful is when logically individual bit-set can be use collectively. The most common example is bit-packed flags (booleans).

There is always the tradeoff between Memory and Performance.

Optimize one, affects the other.
(eg. use less Memory, have less performance. Use more memory have higher performance)

No longer generally true. eg. Use less memory and have higher performance, use more memory and be slower.