"Vitaliy" wrote: > > > I've heard about interesting trick shifting bits individually > > > into different positions by multiplying on constant on micros > > > with hardware multiplication unit. > > > > Pretty simple, actually. A left shift (towards the MSB) is the > > same as a multiply by 2. Each subsequent by 2 is a shift. > > This is obvious, but I think Dmitriy had something else in mind: > "shifting bits individually into different positions." The keyword is > "individually", I don't see a reason to use hardware multiply if you can > use shift left/shift right. Shift left/shift right on most microcontrollers only shift by one bit at a time. Most DSPs have a unit called a "barrel shifter" (a huge array of multiplexers) that can shift by any number of bits in constant time. The "trick" is that a hardware multiplier can be used as a barrel shifter by using the correct factors. If you have a multiply-accumulate instruction, you can insert a bit (or a bit field) into a word in one instruction cycle. You can even spread a bit or bitfield value over multiple positions if you get clever. This is something that a plain barrel shifter can't do. For example, suppose you have an 8-bit value that represents a grayscale pixel. You can convert this to the equivalent RGB value packed into a 32-bit word by multiplying it by 0x00010101. If you want it left-justified in the word, just change the constant to 0x01010100. -- Dave Tweed -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist