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

Question for "<<"

Hi all

I write a sample code like

a = 1 << b;

when b = 0 , The result for a will get 2

If I uncheck "Enable ANSI integer promotion rules " ,It will get correct answer 1

can anyone tell me why ??

Thanks a lot

Parents
  • The section of your code that does the shifting is this bit:

    0024 7401   MOV A,#01H
    0026 A807   MOV R0,AR7
    0028 08     INC R0
    0029 8002   SJMP ?C0181
    002B ?C0180:
    002B C3     CLR C
    002C 33     RLC A
    002D ?C0181:
    002D D8FC   DJNZ R0,?C0180
    002F FF     MOV R7,A
    
    That looks OK to me. This is an 8-bit shift so presumably this is the code without ANSI promotion rules.

    Is the code you posted the version that works?

    In the past, I have spent some time looking at the shift operators in C51 and they always looked OK to me.

    You might find the following code to be of interest, although it only relates to 16 and 32-bit shift and roll operations.

    http://www.programmersheaven.com/zone5/cat27/31937.htm

Reply
  • The section of your code that does the shifting is this bit:

    0024 7401   MOV A,#01H
    0026 A807   MOV R0,AR7
    0028 08     INC R0
    0029 8002   SJMP ?C0181
    002B ?C0180:
    002B C3     CLR C
    002C 33     RLC A
    002D ?C0181:
    002D D8FC   DJNZ R0,?C0180
    002F FF     MOV R7,A
    
    That looks OK to me. This is an 8-bit shift so presumably this is the code without ANSI promotion rules.

    Is the code you posted the version that works?

    In the past, I have spent some time looking at the shift operators in C51 and they always looked OK to me.

    You might find the following code to be of interest, although it only relates to 16 and 32-bit shift and roll operations.

    http://www.programmersheaven.com/zone5/cat27/31937.htm

Children