Advertisement

Fastest Method

Started by September 16, 2000 07:49 PM
13 comments, last by Esap1 24 years, 3 months ago
I wanna place 8 bits in a 32 bit type is a certain place The fastest possible way. For example, this is my code: This would fill up a 32 variables with 4 8 bit variables

ints = ints + (in4<<24);
ints = ints + (in3<<16);
ints = ints + (in2<<8);
ints = ints + in1;
 
Is there any faster way, like OR, or XOR, or some of those?(The only one I know how to use is AND).
Is there one?
Advertisement
not sure exactly what you mean, but you could do

ints |= (int4 << 24);
ints |= (int3 << 16);
ints |= (int2 << 8);
ints |= int1;

or you could do

ints = (int4 << 24) | (int3 << 16) | (int2 << 8) | int1;

but i dont think theres much of a speed difference, if at all, between the way you said and these

Thanks, I had another question, are unsigned short''s(or just shorts) 8 bit, if not, what is?
shorts are 16bit i think

char''s are 8 bit
hence the
typedef unsigned char BYTE;

8 bits = 1 byte
char is 8 bits
short is 16 bits
int is 16 bits
long is 32 bits
long long (C99 only) is 64 bits
float is 32 bits
double is 64 bits
long double is 80 or 64 bits (compiler dependent)

------------------------------
#pragma twice


sharewaregames.20m.com

Advertisement
note that an int has an undefined size and can be different from compiler to compiler. On a 32 bit compiler, which is what most of you are using, and int is 32 bits.

cmaker

- its not the principle. its the money.
cmaker- I do not make clones.
I ran into that problem before. The compilers at school had 16bit integers, and my compiler at home had 32 bit integers. We had to read in a file given to us by the professor. Took me a long time to figure out why the list of supposed 5 digit numbers looked like this to my program...
1023020103
1023832929
1010293010
1000102039
etc....
quote: Original post by Esap1

I wanna place 8 bits in a 32 bit type is a certain place The fastest possible way. For example, this is my code:
This would fill up a 32 variables with 4 8 bit variables
ints = ints + (in4<<24);ints = ints + (in3<<16);ints = ints + (in2<<8);ints = ints + in1;  


Is there any faster way, like OR, or XOR, or some of those?(The only one I know how to use is AND).


Hi all.

If you can use inline assembler, you could write:

__asm{   mov   ah,byte ptr in4   mov   al,byte ptr in3   shl   eax,16   mov   ah,byte ptr in2   mov   al,byte ptr in1   mov   ints,eax} 


Topgoro


;You are not a real programmer until you start all your sentences with a semicolon
We emphasize "gotoless" programming in this company, so constructs like "goto hell" are strictly forbidden.
The only thing you are guaranteed is that:

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

I don''t believe that you are even guaranteed that sizeof(char) == 1 byte. You''re simply guaranteed that it is 1 something.

In practice I''ve never seen a compiler that didn''t do sizeof(char) == 1 byte. It would also break virtually every C/C++ app in the universe if it were not true.

For the vast majority of 32-bit compilers, sizeof(int)=sizeof(long)=32bits. For 16 or 64 bit it is often the case that sizeof(int)!=sizeof(long).

If you really care how many bits things are, you should make a new type that explicitely says it. e.g. typedef short int16. VC has this built in.

-Mike

This topic is closed to new replies.

Advertisement