# Using fixed point notations on dsPIC

#### darkfeffy

Joined Apr 1, 2009
12
Hi
I want to use a dSPIC33f (16-bit) for a closed-loop dc-dc converter application. I have some difficulty understanding certain details about the fixed point notation and conversions. From sample code, I see certain instructions like:

currentReferenceDynamic = SineTable512[((sineAngle) >> 5)];

I need to understand what the operators "<<" and ">>" do in these instructions. Could someone explain please? Any documents/links that explain these kinds of operations?

Thanks.
ed.

#### MrChips

Joined Oct 2, 2009
23,316
<< means shift left.

Hence << 5 is shift left 5 bits which is a quick way to multiply by 32.

Similarly >> 5 means shift right 5 bits which is also integer divide by 32.

#### darkfeffy

Joined Apr 1, 2009
12

#### WBahn

Joined Mar 31, 2012
26,398
These are "shift" operations.

The << is the 'left shift' operator and the expression evaluates to the value of the left operand (the ADCBUF3) left shifted by the right operand (the '5'). So, in this case, the value stored in pvOutputVoltage is what you would get if you wrote the value in ADCBUF3 in binary and then moved them all five places to the left (you lose the high order 5 bits since they 'fall off then end') and you backfill the right hand side with five zeros.

The >> is the 'right shift' operator and is basically the same except the value in sineAngle is shifted to the right five places. The five lowest order bits fall off the end and are lost. There is a subtlety, however, that can bite you. Depending on the language and the data type of the argument, the backfill that occurs on the right could either be all zeros (making it a logical right shift) or the most significant bit (the sign bit) could be copied into all of the vacated positions (making it an arithmetic right shift). In C, right shifts on unsigned types are logical and right shifts on signed types is arithmetic.

Shifting left by one place is the equivalent of multiplying by 2. So shifting left by 5 places is the same as multiplying by 2 five times, or by 2^5=32. Right shifting by one place is roughly equivalent to dividing by 2, so right shifting by two places is the same as dividing by 32. However, right shifting and division are not exactly the same in the case of negative integers. In C, the standard does not specify what a compiler does in this case, it only requires that the compiler specify what it happens to do.

In the case of floating point variables, this is treated by most compilers as an illegal operation. I don't recall if that is required by the standard, or if the behavior is undefined.

#### darkfeffy

Joined Apr 1, 2009
12
Shifting left by one place is the equivalent of multiplying by 2. So shifting left by 5 places is the same as multiplying by 2 five times, or by 2^5=32

#### MrChips

Joined Oct 2, 2009
23,316
That depends on the compiler. An optimizing compiler might recognize that *32 can be implemented as << 5 which is usually faster.

Or if the MCU architecture has hardware multiply or divide, doing the integer multiply or divide could be just as fast. Hence a lot depends on the compiler as well as the processor hardware.

Performing 5 shift operations in assembler is always faster than integer multiply or divide in software.

#### WBahn

Joined Mar 31, 2012
26,398