Похожие чаты

I am puzzled by a Java data type question. I

would assume that a primitive boolean would only take 1 bit of storage, but it seems it takes 8 bits (according to Oracle JVM doc)

Moreover, the Oracle tutorial says that the size is not precisely defined. I understand that other JVM implementations may have different implementation for booleans, but why? This makes it so much more confusing

15 ответов

57 просмотров

It is impossible to fetch a singke bit from memory, most assembly languages do not support that. To store a boolean as a single bit would come with much memory management effort. Such ways of data access exist, for example in C++ with std::vector<bool>, but are not suitable for a general use data type as a primitive boolean.

If you want to optimize for storage capability, you can store your bits in bytes and evaluate each position using bitmasks. Only really valuable if you have > 8*<length of your unpacking code> booleans to store

Roy- Автор вопроса

The reason that boolean sizes aren't defined is that its not relevant for functionality but that you want booleans to fetch as quickly as possible. X86 can fetch BYTE, WORD, DWORD, QWORD and is optimized for fetching blocks of four (QWORD) - hence why you want to align your structures on qword boundaries

BinaryByter
The reason that boolean sizes aren't defined is th...

https://cdrdv2-public.intel.com/671488/248966-046A-software-optimization-manual.pdf See chapter 3.4.1.4

Roy- Автор вопроса
BinaryByter
The reason that boolean sizes aren't defined is th...

got it. We (I) usually never have to think on this low level. Mechanical un-sympathetic 😂

BinaryByter
do this

In c++ its a structure called bitset https://cplusplus.com/reference/bitset/bitset/

Roy- Автор вопроса

Basically, you need many bools

Roy- Автор вопроса
Roy
Java also has it, no?

Javas Bitset does not optimize for size

Roy- Автор вопроса
Roy
yes, let's ignore that for now

Do we want to optimize for size or no?

Roy- Автор вопроса

Похожие вопросы

Обсуждают сегодня

Карта сайта