C语言里的算术移位和逻辑移位运算例程

  • From: "Vincent Silent" <vincent.silent@xxxxxxxxx>
  • To: ghostunix@xxxxxxxxxxxxx
  • Date: Fri, 16 May 2008 11:59:04 +0800

#include <stdio.h>

int sxl(int i, int c)
{
    return i << c;
}

int sar(int i, int c)
{
    return i >> c;
}

int shr(int i, int c)
{
    register int _res;
    __asm__("movl %2, %%ecx; \
             shrl %%cl, %1; \
             movl %1, %0"
            : "=a"(_res)
            : "r"(i), "r"(c));
    return _res;
}

int main(void)
{
    printf("sxl(-8, 2) = %d\n", sxl(-8, 2));
    printf("sar(-8, 2) = %d\n", sar(-8, 2));
    printf("shr(-8, 2) = %d\n", shr(-8, 2));
    return 0;
}


From : http://akumo.org/?p=31

Attachment: tttt.png
Description: PNG image

Other related posts: