当前位置:网站首页>Shift Operators

Shift Operators

2022-07-06 08:34:00 There is no place to find a bug after treading on iron shoes. I

One 、 Move left :<<
Move the complement of a number to the left , use 0 fill

#include <stdio.h>
int main()
{
    
    char a = 65;
    char c = a<<1;
    printf("%d", c);
    return 0;
}

The result is :-126

 Positive numbers :65
 The positive number is the same as the inverse complement :01000001
 Result after left shift :10000010	// This is the original code after moving , Need to be converted to complement 
 Inverse code :11111101
 Complement code :11111110
 result :-126


 negative :-65
 Original code :11000001
 Inverse code :10111110
 Complement code :10111111
 Move to the left :01111110		// The original code is a positive number , The former is the same as the latter 
 result :126

Two 、 Move right :>>
Move the complement of the number to the right , For unsigned numbers , use 0 fill , For signed numbers , Fill with sign bits .

#include <stdio.h>
int main()
{
    
    char a = -7;
    char c = a>>2;
    printf("%d", c);
    return 0;
}

The result is -2

 negative :-7
 Original code :10000111
 Inverse code :11111000
 Complement code :11111001
 The result after moving right :11111110		// This is the original code after moving , Need to be converted to complement 
 Inverse code :10000001
 Complement code :10000010
 result :-2

 Positive numbers :7
 The positive number is the same as the inverse complement :00000111
 The result after moving right :00000001		// This is the original code after moving , It's a positive number , The former is the same as the latter 
 result :1
原网站

版权声明
本文为[There is no place to find a bug after treading on iron shoes. I]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060822556135.html