当前位置:网站首页>(4) Golang operator
(4) Golang operator
2022-06-12 17:08:00 【Binary cup don't stop】
( Four )Golang Operator
One 、 Operator
1.1 Arithmetic operator
+ - * / %( Remainder ) ++ --
Use cases :
package main
import "fmt"
func main(){
/* Arithmetic operator : + - * / % ++ -- */
a := 10
b := 3
sum := a + b
fmt.Printf("%d + %d = %d\n", a, b, sum)
sub := a - b
fmt.Printf("%d - %d = %d\n", a, b, sub)
mul := a * b
fmt.Printf("%d * %d = %d\n", a, b, mul)
div := a / b // Take business
fmt.Printf("%d / %d = %d\n", a, b, div)
mod := a % b // Remainder
fmt.Printf("%d %% %d = %d\n", a, b, mod)
c := 3
c ++
fmt.Println("c = ", c)
d := 3
d --
fmt.Println("d = ", d)
}
Console output :
$ go run xxx.go
10 + 3 = 13
10 - 3 = 7
10 * 3 = 30
10 / 3 = 3
10 % 3 = 1
c = 4
d = 2
1.2 Relational operator
== != > < >= <=
1.3 Logical operators
&& || !
1.4 An operator
& | ^ &^ << >>
1.4.1 An operation
| A | B | A&B | A|B | A^B |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 |
Use cases : hypothesis A by 60,B by 13
| operation | describe | Example |
|---|---|---|
| & | The result of binary and operation copy bits , If it exists in two operands | (A&B) = 12, That is to say 0000 1100 |
| | | Binary or operational copy , If it has an operand | (A|B) = 61, That is to say 0011 1101 |
| ^ | Binary operation copy , If it is set to an operand, it is bitwise not | (A^B)=49, That is to say 0011 0001 |
| &^ | Binary bit emptying &^ | (A&^B)=48, That is to say 110000 |
Bit empty :$^
- about
a $^ b - about b Each value on the
- If 0, Then take a The value on the corresponding bit
- If 1, Then the result bit is 0
package main
import "fmt"
func main(){
/* Arithmetic operator : + - * / % ++ -- */
a := 60
b := 13
/* a: 60 0011 1100 b: 13 0000 1101 & 0000 1100 | 0011 1101 ^ 0011 0001 &^ 0011 0000 */
fmt.Printf("a:%d, %b\n",a,a)
fmt.Printf("b:%d, %b\n",b,b)
res1 := a & b
fmt.Printf("res1:%d, %b\n",res1,res1) //12
res2 := a | b
fmt.Printf("res2:%d, %b\n",res2,res2) //61
res3 := a ^ b
fmt.Printf("res3:%d, %b\n",res3,res3) //49
res4 := a &^ b
fmt.Printf("res4:%d, %b\n",res4,res4) //48
}
Console output :
$ go run xxx.go
a:60, 111100
b:13, 1101
res1:12, 1100
res2:61, 111101
res3:49, 110001
res4:48, 110000
1.4.2 Displacement operators
<<: Move left according to position , take a Change to binary , Move to the left b position , Equivalent to multiplying 2 Of b Power
>>: Right shift to position , take a Change to binary , To the right b position , Equivalent to dividing 2 Of b Power
Use cases :
c := 8
res5 := c << 2
fmt.Printf("res5:%d, %b\n",res5,res5) //32
res6 := c >> 2
fmt.Printf("res6:%d, %b\n",res6,res6) //2
Console output :
res5:32, 100000
res6:2, 10
1.5 Assignment operator
| Operator | describe | Example |
|---|---|---|
| = | Assignment operator | C = A + B, Distribute A+B Value to C |
| += | Add and assign operator | C += A, amount to C=C+A |
| -= | Subtract and assign | C-=A, amount to C=C-A |
| *= | Multiply and assign | C*=A, amount to C=C*A |
| /= | Divide and assign | C/=A, amount to C=C/A |
| %= | Take remainder and assign value | C%=A, amount to C=C%A |
| <<= | Move left and assign | C<<=2, amount to C=C<<2 |
| >>= | Move right and assign | C>>=2, amount to C=C>>2 |
| &= | Bitwise and union assignment | C&=2, amount to C=C&2 |
| |= | Bitwise OR and assign values | C|=2, amount to C=C|2 |
边栏推荐
- Picture online collection and delivery system source code
- R语言使用pdf函数将可视化图像结果保存到pdf文件中、使用pdf函数打开图像设备、使用dev.off函数关闭图像设备、自定义width参数和height参数指定图像的宽度和高度
- Uniapp壁纸小程序源码/双端微信抖音小程序源码
- Latex table online generation
- Sudo of uabntu
- 两位新晋Committer的“升级攻略”
- Male god goddess voting source code v5.5.21 voting source code
- Dongfeng Yueda Kia, Tencent advertising and hero League mobile game professional league cooperate to build a new E-sports ecology
- 5、Embedding
- Qt开发高级进阶:初探qt + opengl
猜你喜欢

Nebula's practice of intelligent risk control in akulaku: training and deployment of graph model

redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required

云开发坤坤鸡乐盒微信小程序源码

Doctor application | National University of Singapore, Xinchao Wang, teacher recruitment, doctor / postdoctoral candidate in the direction of graph neural network

Idea how to set the guide package without * sign

使用GCC的PGO(Profile-guided Optimization)优化整个系统

Atlassian Confluence 远程代码执行漏洞(CVE-2022-26134)漏洞复现

Some minor problems and solutions encountered when using ubantu

Recommend AI intelligent drawing repair software

Idea displays services on the console to uniformly manage all jetty services,
随机推荐
Modify the configuration of the router connected to your computer. The website is 192.168.1.1
pytorch和torchvision官方文档使用方法
Pat class a 1142 largest regiment
Microsoft Office MSDT代码执行漏洞(CVE-2022-30190)漏洞复现
A variety of Qt development methods, which do you choose?
Some minor problems and solutions encountered when using ubantu
薛定谔的日语学习小程序源码
Download PHP source code of leaf sharing station
(五)输出和输出
Uniapp wallpaper applet source code / double ended wechat Tiktok applet source code
Nebula's practice of intelligent risk control in akulaku: training and deployment of graph model
反馈式编译
初识GO语言
Golang recursively encrypts and decrypts all files under the specified folder
Sudo of uabntu
使用GCC的PGO(Profile-guided Optimization)优化整个系统
The safety of link 01 was questioned, and "ultra high strength" became "high strength"_ Publicity_ Steel_ problem
The R language uses the tablestack function of epidisplay package to generate statistical analysis tables based on grouped variables (including descriptive statistical analysis, hypothesis test, diffe
Gerrit+2触发Jenkins任务
R语言使用epiDisplay包的tableStack函数基于分组变量生成统计分析表(包含描述性统计分析、假设检验、不同数据使用不同的统计量和假设检验方法)、自定义配置是否显示统计检验内容