Sign extension with bitwise shift operation

前端 未结 5 1298
小蘑菇
小蘑菇 2021-01-06 02:23

following this Q&A I tried to exam the answer so I wrote:

#include 

int main ()
{

        int t;int i;
        for (i=120;i<140;i++){         


        
5条回答
  •  轮回少年
    2021-01-06 02:54

    The >> operator, right shift, is arithmetic right shift in most compilers, meaning divide-by-2.

    So, if, e.g. int i ==-4 (0xfffffffc), then i>>1 == -2 (0xfffffffe).

    Having said this, I would recommend you to check the assembly of your code.
    e.g. x86 has 2 separate instructions - shr & sar, denoting logical shift & arithmetic shift respectively.
    Generally, compilers use shr (logical shift) for unsigned variables & sar (arithmetic shift) for signed variables.


    Below is the C code & corresponding assembly, generated with gcc -S:

    a.c:

    int x=10;
    unsigned int y=10;
    
    int main(){
        unsigned int z=(x>>1)+(y>>1);
        return 0;
    }
    

    a.s:

        .file   "a.c"
    .globl x
        .data
        .align 4
        .type   x, @object
        .size   x, 4
    x:
        .long   10
    .globl y
        .align 4
        .type   y, @object
        .size   y, 4
    y:
        .long   10
        .text
    .globl main
        .type   main, @function
    main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $16, %esp
        movl    x, %eax
        sarl    %eax ; <~~~~~~~~~~~~~~~~ Arithmetic shift, for signed int
        movl    y, %edx
        shrl    %edx ; <~~~~~~~~~~~~~~~~ Logical shift, for unsigned int
        addl    %edx, %eax
        movl    %eax, -4(%ebp)
        movl    $0, %eax
        leave
        ret
        .size   main, .-main
        .ident  "GCC: (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2"
        .section    .note.GNU-stack,"",@progbits
    

提交回复
热议问题