当前位置:网站首页>Detailed explanation of shift operator and bit operator in C language
Detailed explanation of shift operator and bit operator in C language
2022-06-11 07:44:00 【Fried tomatoes 110】
Catalog
1、 Shift operator
It is divided into the shift left operator (<<) And the shift right operator (>>).
Shift left operator :
Shift rules : Abandon on the left , The right to repair 0.
What does that mean , Now let me explain it in detail .
For example, we define an integer variable a, Give it an initial value 5. Such as int a=5; And then a Move left , namely a<<2, What is the result ? Before we talk about it , We first need to understand integer types 5 How is it stored in memory , If you don't know, please read my previous blog The integer 3 Expression and storage in memory La , There are detailed explanations . integer 5 It is stored in the form of complement . And will be 5 Move left 2 Bit is to move the binary bits stored in memory to the left 2 Bit and put this 2 Bit discard , Right side use 0 A filling . The last thing I got was 5<<2 The result after , The following figure can be used to briefly describe ~

According to the picture above a<<2 The result is 20.
Shift right operator :
First, the right shift operation is divided into two types . Arithmetic shift and logical shift .
Arithmetic shift rule : The left side is filled with the sign bit of the value , On the right side of the discarded .
Logical shift rules : Use... On the left 0 Add , On the right side of the discarded .
What do you mean by that , Let's take a look at a few examples , Such as the 5 Shift right , namely 5>>1, How much do you get ?
If it is an arithmetic shift , Will be 5 The bits stored in memory move to the right 1 And throw this one away , On the left, the sign bits of the binary bits stored with this value are complemented . Here's the picture :

therefore 5>>1 The result is 2. What about logical shift , A logical shift is a left-hand shift 0, Add , Right abandon . For positive numbers , Its sign bit is 0, So the result of arithmetic shift is the same as that of logical shift , Logical shift can also be represented by the figure above , The result is also 2.
If the -5 One bit to the right , What is the result . Let's first look at arithmetic shifts , I won't go into details , Please look at the chart below. :

therefore -5>>1 The result of the arithmetic shift is -3.
Empathy , Let's see -5>>1 What is the result of logical shift ~

Because the highest position is 0, So the end result is 3 La ~. But in general, we use the arithmetic shift .
2、 Bit operators
& Bitwise ( Binary digit ) And
| Bitwise ( Binary digit ) or
^ Bitwise ( Binary digit ) Exclusive or
Let's look at this code directly , You can first guess which three numbers are finally output :
int main()
{
int a = 3;
int b = -5;
printf("%d %d %d", a & b, a | b, a ^ b);
return 0;
}Let's analyze first : Bitwise AND Is to compare the binary bits stored in two numbers , Both are 1 Only then 1, Otherwise 0. therefore a&b Analysis by graph is :

therefore a&b The result is 3 了 .
Press bit or Namely Compare the binary bits stored in two numbers , One of the two is 1 for 1, Otherwise 0. therefore a|b Analysis by graph is :

therefore a|b The value is -5 了 .
Bitwise XOR Namely Compare the binary bits stored in two numbers , The two are the same 0, Dissimilarity is 1, therefore a^b Analysis by graph is :

therefore a^b The result is that -8 La .
Finally, let's let the program run :

It is the same as the analysis ~~~
It's over here , I hope you can give me a compliment ~~~
边栏推荐
- [untitled] Weng_ C lesson 1
- C language - Growth Diary -03- function definition and function prototype declaration
- Sqlzoo question brushing record-3
- Remote office experience | community essay solicitation
- Post-processing of ffmpeg miscellaneous notes
- [Oracle database] mammy tutorial day02 use of database management tool sqlplus
- 零基础自学SQL课程 | OUTER JOIN外连接
- 远程办公经验分享 | 社区征文
- [atcoder2306] rearranging (topology)
- C language three chess games
猜你喜欢

Miscellany C language

C language - Growth Diary -01- count primes and sum

C language - Growth Diary -03- function definition and function prototype declaration

【AtCoder2306】Rearranging(拓扑)

C memory alignment

Zero foundation self-study SQL course | outer join external connection

mpi

You got 8K in the 3-year function test, but you were actually pretending to work hard

C language Yanghui triangle code

forEach 中 return 和 for 中 break
随机推荐
【AtCoder2000】Leftmost Ball (DP+组合数)
Remote office experience | community essay solicitation
20200730 T3 small B farm [maximum perimeter empty rectangle (monotone stack + line segment tree)] & "ROI 2017 day 2" learning track
MFC auxiliary CString string splicing
Compound ratemodel contract analysis
Nim product
The maximum number of divisors of numbers in the int range is 1536
二本畢業,銀行外包測試工作 4 個月有餘。聊聊一些真實感受 ...
【AtCoder1998】Stamp Rally(整体二分+并查集)
【AtCoder1984】Wide Swap (拓扑排序转化)
Sort - select sort
Post-processing of ffmpeg miscellaneous notes
【AtCoder2376】Black and White Tree(博弈)
20200727 T2 small w playing game [generating function (binomial inversion technique)]
Sqlzoo question brushing record-3
20200810 T2 dispatch money
Uoj 554 [unr 4] challenges Hamilton [find Hamilton path (adjustment method)]
Summary of classic interview questions
Find the longest common substring of N arrays (strings)
20200803 T3 my friends [divide and conquer NTT optimization recursion]