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...
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...