运算 Kotlin(3)

运算
Kotlin支持数字运算的标准集,运算被定义为相应的类成员(但编译器会将函数调用优化为相
应的指令) 。 参见运算符重载
对于位运算,没有特殊字符来表示,而只可用中缀方式调用命名函数,例如:
val x = (1 shl 2) and 0x000FF000
这是完整的位运算列表(只用于 Int Long ) :
shl(bits) 有符号左移 (Java << )
shr(bits) 有符号右移 (Java >> )
ushr(bits) 无符号右移 (Java >>> )--------------无符号左移?
and(bits) 位与
or(bits) 位或
xor(bits) 位异或
基本类型
55
inv() 位非



 两种写法

var a = 1000
a = a.shl(1)
println(a)

var b = 1000
b = a shl 1
println(b)
二进制写法:0b开头。‘零b’
var c = 0b1100110
c = c and 1111111
c = c.and(0b11110000)
println("c $c")





原文地址:https://www.cnblogs.com/mamamia/p/8384608.html