Advertisement

Bitwise shifting question...

Started by March 06, 2000 10:29 PM
2 comments, last by Bigshot 24 years, 6 months ago
Why can''t I shift an element of an array? Take this code for example: cos_lookup[angle] << 7; Using << 7 is supposed to be equivalent to multiplying by 128 (but faster), but when I try to compile the code I get the errors ''bad left operand'' and ''bad right operand''. What''s up with this crap? Is there any way I can use shifting on an element of an array without losing any speed? I know i could copy the element to another integer and perform the shift on the new integer, but wouldn''t all that end up taking more cpu time than just multiplying the array element by 128? I need answers! And I''d appreciate them, Al
Alexbigshot@austin.rr.comFoolish man give wife grand piano. Wise man give wife upright organ.
Couple of thoughts...
First thought: Did you try wrapping your cos_lookup[angle] with parenthesis as in:
(cos_lookup[angle]) << 7;

Second thought: shifting by 7 only multiplies by 128 for integer types, not floating types, so if your cos_lookup array isn''t an int type, you''ve got troubles.

Third thought: Mosy optimizing compilers will transform an int * 128 into int << 7 after it compiles anyway.
Advertisement
Crap, my look-up table is floating point! I knew that you could only shift integers, but I forgot that my look-up table stores float numbers, not integers. Hehe, oh well. I could try casting the number, but I''ll probably just stick with *128.

Thanks,
Al
Alexbigshot@austin.rr.comFoolish man give wife grand piano. Wise man give wife upright organ.
It''s good that you found you weren''t using the right data type, but I have a problem with this:
quote:
Using << 7 is supposed to be equivalent to multiplying by 128 (but faster)

Actually, the compiler my grandmother coded with one hand will compile (i <<= 7) and (i *= 128) to the exact same code. And she''s dead.

If you''re working in assembly, you should use a shift (LSH?) rather than a multiply (IMUL? been too long). If you''re working in C, they compile to exactly the same code. Check the disassembly if you don''t believe me.

Wow, my tone''s really sarchastic this morning. Maybe I should have my first cup of joe before replying. hmmm.

This topic is closed to new replies.

Advertisement