This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

shift with ">>" in c51 ...

some time c user do shift for mul and divide and some time u write byte to byte
then not require any carry for me in asm i do two shift mul 4

i do it with c51

ypos3 = ypos>>3;
; SOURCE LINE # 237
MOV A,R5
RRC A
RRC A
ANL A,#01FH
;---- Variable 'ypos3?1255' assigned to

HOW DO IT IN C51 WITHOUT
MOV A,R5
ANL A,#01FH

??

Parents
  • I am not sure that I really understand your question, but I hope that this helps.

    In general, when you write a C statement that includes arithmetic operations multiply, divide and modulo where one operand is a constant that is 2^n, the Keil compiler is good at converting the operation to the shift, SWAP and mask equivalent. In most cases, the programmer is best advised to use the operators that make the best sense to a human reader and leave the rest to the compiler.

    You cannot tell the compiler to do things any particular way. The only alternative is to resort to assembly language...

    It is possible to achieve shift operations by multiplying or dividing. In cases when the number of bits to shift is variable, this can work out faster than bit shifting. To shift n bits to the left or right, multiply or divide by 2^n.

    When shifting by a variable number of bits, Keil C51 does not do so well. Keil also offers some some "intrinsic" functions for doing rolls, these are not efficient when shifting for more than about 3 bits. In both these cases, some hand optimisation may be worthwhile.

    This is particularly true when rolling long variables. See:

    http://www.keil.com/forum/docs/thread1252.asp

    for an example. The methods used in the example can easily be adapted to provide solutions that shift or roll, longs, ints or chars.

Reply
  • I am not sure that I really understand your question, but I hope that this helps.

    In general, when you write a C statement that includes arithmetic operations multiply, divide and modulo where one operand is a constant that is 2^n, the Keil compiler is good at converting the operation to the shift, SWAP and mask equivalent. In most cases, the programmer is best advised to use the operators that make the best sense to a human reader and leave the rest to the compiler.

    You cannot tell the compiler to do things any particular way. The only alternative is to resort to assembly language...

    It is possible to achieve shift operations by multiplying or dividing. In cases when the number of bits to shift is variable, this can work out faster than bit shifting. To shift n bits to the left or right, multiply or divide by 2^n.

    When shifting by a variable number of bits, Keil C51 does not do so well. Keil also offers some some "intrinsic" functions for doing rolls, these are not efficient when shifting for more than about 3 bits. In both these cases, some hand optimisation may be worthwhile.

    This is particularly true when rolling long variables. See:

    http://www.keil.com/forum/docs/thread1252.asp

    for an example. The methods used in the example can easily be adapted to provide solutions that shift or roll, longs, ints or chars.

Children