Data Type Size Question

I

Immortal Nephi

C++ Compiler provides four data type sizes (char, short, long, and
long long). Sometimes, programmers prefer 32 bit size over 16 bit
size or 8 bit size. They say 16 bit size is slower than 8 bit size
and 32 bit size on x86 machine.

What choice do programmers prefer? For example.

unsigned char byte = 0x03;
byte++;

unsigned short byte = 0x03;
....or...
unsigned long byte = 0x03;

byte++;
byte &= 0xFF;

If you choose unsigned char, then you don't need to add "byte &=
0xFF".

unsigned short word = 0x2003;
word++;

unsigned long word = 0x2003;

word++;
word &= 0xFFFF;

If you choose unsigned short, then you don't need to add "word &=
0xFFFF".

Sometimes, C++ Compiler's optimization loads data from either 8 bit
variable or 16 bit variable into 32 bit register. The x86
instructions work to do calculation on 32 bit register before it is
stored back to 8 bit variable or 16 bit variable. It might cost more
clock cycles.

Sometimes, programmers choose two 8 bit variables instead of one 16
bit variable. For example

unsigned char low_byte = 0x03;
unsigned char high_byte = 0x20;

unsigned short word = 0x2003;

Programmers can work to modify high byte directly. They don't need to
use right 8 bit shift ( high_byte >> 8 ) to modify high byte before it
is left 8 bit shift back to 16 bit variable.

If they choose 16 bit variable instead of 8 bit variable, then they
are required to use both left 8 bit shift and right 8 bit shift
because they don't use them very frequently.

Please let me know the best practices they prefer. Thanks...
 
R

red floyd

Immortal said:
C++ Compiler provides four data type sizes
correct
(char, short, long, and
long long).

Incorrect, at least for C++03. Long long is not defined in C++03, but
it is in C++0x.


In C++03 the sizes are char, short, int and long.
 
I

Immortal Nephi

correct
  (char, short, long, and


Incorrect, at least for C++03.  Long long is not defined in C++03, but
it is in C++0x.

In C++03 the sizes are char, short, int and long.

Is Microsoft C++ Compiler (from Visual Studio .Net 2008) C++03?

I thought that int can be either 16 bit or 32 bit on 32 bit machine
and either 32 bit or 64 bit on 64 bit machine. Correct?

Can you please tell me your opinion? Which data type do programmers
prefer? If they don't want to use "&= 0xFF" or "&= 0xFFFF" then they
choose either char or short size. If they don't use ">> 8" and "<< 8"
very frequent, then they choose two char variables instead of one
short variable or int variable. Thanks...
 
F

Fred

Some compilers support long long as an extension.

What about float and doubble?

I thought the int-type sizes were only required to follow:

sizeof(short) <= sizeof(int) <= sizeof(long)

All three *could* be the same length
 
C

Camiel

With the char, short, int, long and long long data types there are no
guarantees as to their size, except that char <= short <= int <= long
<= long long. Its up to the compiler writer to choose sizes that make
sense on the platform. For example, on 36-bit machines (PDP-10 for
instance) it was not uncommon to have a 9-bit char type.

I write hardware emulators for a living, so I often really _do_ care
about how many bits my data types are. If that is the case, and if the
code needs to be portable, I will include a separate header file to
define my own data types based on what data types the compiler has to
offer. If available, I will use the C99 int8_t, int16_t... data types,
or Microsoft's __int8, __int16.... I then use whatever fits. If the
value I'm working with is an 8-bit value, I'll use the 8-bit data type
I defined.
 
B

Bart van Ingen Schenau

Immortal said:
C++ Compiler provides four data type sizes (char, short, long, and
long long).

You are missing int, and long long is not required to be available.
Sometimes, programmers prefer 32 bit size over 16 bit
size or 8 bit size. They say 16 bit size is slower than 8 bit size
and 32 bit size on x86 machine.

What choice do programmers prefer?

Use the type that most clearly expresses your intent.
The only reason to use one of the tricks you showed is because
measurements have shown that the code with the original type was causing
a bottleneck in the program and these changes gave a measurable
improvement.

Bart v Ingen Schenau
 
J

James Kanze

C++ Compiler provides four data type sizes (char, short, long,
and long long).

The current C++ standard provides for up to four sizes for
integral data: char, short, int and long. The next version of
the standard (and the current C standard) provide for more: they
requrie support for long long, and allow an implementation to
define additional integral types.

Of course, there's no requirement that any of these types
actually have different sizes; it's quite frequent for short,
int and long to all have the same size, for example, and I've
heard that there are embedded processors where even char has the
same size as the other types.

And, of course, pointers, floating point, etc. may have still
other sizes.
Sometimes, programmers prefer 32 bit size over 16 bit size or
8 bit size. They say 16 bit size is slower than 8 bit size
and 32 bit size on x86 machine.
What choice do programmers prefer?

Normally, unless there are specific requirements otherwise, a
programmer should just use int, and be done with it. That's
what it's there for.
For example.
unsigned char byte = 0x03;
byte++;
unsigned short byte = 0x03;
...or...
unsigned long byte = 0x03;
byte++;
byte &= 0xFF;
If you choose unsigned char, then you don't need to add "byte
&= 0xFF".

That depends. If you're not worried about portability to
machines with bytes sizes other than 8, fine. If you want
exactly 8 bits, however, you need the & 0xFF in truly portable
code. And of course, if the byte size is 8, I would expect the
compiler to generate exactly the same code whether you wrote the
&= 0xFF or not.
unsigned short word = 0x2003;
word++;
unsigned long word = 0x2003;
word++;
word &= 0xFFFF;
If you choose unsigned short, then you don't need to add "word
&= 0xFFFF".

Provided that short is 16 bits. It often isn't.
Sometimes, C++ Compiler's optimization loads data from either
8 bit variable or 16 bit variable into 32 bit register. The
x86 instructions work to do calculation on 32 bit register
before it is stored back to 8 bit variable or 16 bit variable.
It might cost more clock cycles.
Sometimes, programmers choose two 8 bit variables instead of
one 16 bit variable. For example
unsigned char low_byte = 0x03;
unsigned char high_byte = 0x20;
unsigned short word = 0x2003;
Programmers can work to modify high byte directly. They don't
need to use right 8 bit shift ( high_byte >> 8 ) to modify
high byte before it is left 8 bit shift back to 16 bit
variable.

So? When is that relevant.

The only time I've seen programmers using multiple bytes instead
of a larger integral type is when marshalling, in order to not
have to worry about byte order and alignment.
If they choose 16 bit variable instead of 8 bit variable, then
they are required to use both left 8 bit shift and right 8 bit
shift because they don't use them very frequently.
Please let me know the best practices they prefer. Thanks...

The best practice is to use int for integral values, char for
characters, unsigned char for raw memory, and only use the other
integral types when there are specific constraints (int not
large enough, etc.).
 
J

James Kanze

correct
(char, short, long, and
Incorrect, at least for C++03. Long long is not defined in
C++03, but it is in C++0x.
In C++03 the sizes are char, short, int and long.
[/QUOTE]
Is Microsoft C++ Compiler (from Visual Studio .Net 2008) C++03?
I thought that int can be either 16 bit or 32 bit on 32 bit
machine and either 32 bit or 64 bit on 64 bit machine.
Correct?

int can be anything 16 bits or more. I personally know of cases
where it is 16, 32, 36 and 48; I think other values have also
occured.
Can you please tell me your opinion? Which data type do
programmers prefer?

For integral types, int is the normal type---anything else is
only used for special cases.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top