E
E
Evgeny Ivanov2020-08-03 20:56:56
Programming
Evgeny Ivanov, 2020-08-03 20:56:56

Why are all data types even numbers?

For example in C#
byte: stores an integer from 0 to 255 and occupies 1 byte.
ushort: stores an integer from 0 to 65535 and occupies 2 bytes.
uint: stores an integer from 0 to 4294967295 and occupies 4 bytes.
---
If we need the number 65536, then ushort will not be enough 1 bit. And we will use uint.
"Effectively" we will use only 1 byte. His beat, which we missed so much. We don't need the second byte.

It is logical that there be a data type of 3 bytes. But there is no such data type. But there are 4, 8, 16.
And for a number of "8 bytes + 1bit" we are forced to use 16 bytes. Although they could 9.

And we do, and this is considered the norm. There are no other options.
At the same time, when "number" 255 is given the type ushort, it is considered incorrect. Waste of resources/memory. Yes, and it is not logical and confuses the programmer.

Why are all data types even numbers?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
AnT, 2020-08-03
@logpol32

There are many reasons for this, and the requirements (or recommendations) of data alignment in memory play a significant role in this. For performance reasons, on many (or even most) modern platforms, data must be stored in memory at a boundary that is a multiple of a power of two. So even if you invent a type that consists of 3 bytes, you still have to store data of this type on a 4-byte address boundary for alignment reasons, thereby effectively wasting every fourth byte. In such a situation, it makes no sense to have a 3-byte type if it can be made 4-byte at once, in fact, "for free".
In situations (or platforms) where careful memory saving considerations are more important than performance considerations, your arguments will be stronger. And for such situations, many programming languages ​​provide a means to implement just such more compact types.

G
GavriKos, 2020-08-03
@GavriKos

Because you will not get enough data types under each situation. You need 3 bytes - make an array of three bytes.
Plus, all this is projected into the processor registers, cache, and so on. And int into the register will pass with a whistle, unlike your 5-byte data type. So they optimize the hardware (read even - develop) for fixed dimensions.

I
Igor, 2020-08-18
Bat

The word length in a process is always even.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question