What does a bitwise shift (left or right) do and what is it used for?

前端 未结 9 2166
傲寒
傲寒 2020-12-02 05:00

I\'ve seen the operators >> and << in various code that I\'ve looked at (none of which I actually understood), but I\'m just wondering

9条回答
  •  爱一瞬间的悲伤
    2020-12-02 05:52

    Here is an example:

    #include"stdio.h"
    #include"conio.h"
    
    void main()
    {
        int rm, vivek;
        clrscr();
        printf("Enter any numbers\t(E.g., 1, 2, 5");
        scanf("%d", &rm); // rm = 5(0101) << 2 (two step add zero's), so the value is 10100
        printf("This left shift value%d=%d", rm, rm<<4);
        printf("This right shift value%d=%d", rm, rm>>2);
        getch();
    }
    

提交回复
热议问题