Mathematical Operations and Elementary Functions
Let's provide a complete collection of basic arithmetic and bitwise operators across all of its numeric primitive types, as well as providing portable, efficient implementations of a comprehensive collection of standard mathematical functions.
Arithmetic Operators
The following arithmetic operators are supported on all primitive numeric types:
go
js
dart
python
julia
// ...
var a, b int = 5, 8
a ++ // unary operator can't be assigned
a --
a + b
a - b
a * b
a / b // integer divide
float64(a) / float64(b)
math.Pow(float64(a), float64(b)) // import "math"
a % b // remainder
// ...
let a = 5,
    b = 8
let c = a ++ // c = 5, a = 6
a + b
a - b
a ** b // Math power
a / b
Math.floor(a, b) // integer divide
a % b // remainder
var a = 5, b = 8
// int a = 5; int b = 8;
int e = a++; // e = 5, a = 6
a + b
a - b
print("${pow(a, b)}"); // import "dart:math";
print("${b / a}"); 
print("${b ~/ a}"); // integer divide
a, b = 5, 8
a += 1
a -= 1
a ** b # power
a / b
a // b # integer divide
a, b = 5, 8
c = -a # c will  be -5
a + b
a - b
a / b
a ÷ b # integer divide, type \div then enter, REPL
div(a, b) # in JL file
a ^ b # for powers
Boolean and Bitwise Operators
In addition to the standard numerical operations, there includes operators to perform bitwise logical operations on integers. These are much less commonly used than the standard arithmetic operations, but it's useful to know that they exist. The six bitwise operators are summarized below
go
js
dart
python
julia
var a, b int = 10, 12
^a // not, result: -11
a & b // and, result: 8
a | b // or, result: 14
^(a | b) // nor, result: -15
^(a & b) // nand, result: -9
a ^ b // xor, result: 6
a << b // arithmetic shift left, result: 40960
let a = 10, b = 12
~a // not, result: -11
a & b // and, result: 8
a | b // or, result: 14
~(a | b) // nor, result: -15
~(a & b) // nand, result: -9
(a & ~b) | (~a & b) // xor, result: 6
a << b // arithmetic shift left, result: 40960
var a = 10, b = 12;
~a // not, result: -11
a & b // and, result: 8
a | b // or, result: 14
~(a | b) // nor, result: -15 
~(a & b) // nand, result: -9 
a ^ b // xor, result: 6
a << b // arithmetic shift left, result: 40960
a, b = 10, 12
~a # not, result: -11
a & b # and, result: 8
a | b # or, result: 14
~(a | b) # nor, result: -15
~(a & b) # nand, result: -9
(a & ~b) | (~a & b) # xor, result: 6
a << b #arithmetic shift left, result: 40960
a, b = 10, 12
~a # not, result: -11
a & b # and, result: 8
a | b # or, result: 14
nor(a, b) # REPL, type \nor press tab, result: -15
nand(a, b) # REPL, type \nand press tab, result: -9
xor(a, b) # REPL, type \xor press tab, result: 6
a << b #arithmetic shift left, result: 40960
TypeSigned?Number of bitsSmallest valueLargest value
Int88-2^72^7 - 1
UInt8802^8 - 1
Int1616-2^152^15 - 1
UInt161602^16 - 1
Int3232-2^312^31 - 1
UInt323202^32 - 1
Int6464-2^632^63 - 1
UInt646402^64 - 1
Int128128-2^1272^127 - 1
UInt12812802^128 - 1
BoolN/A8false (0)true (1)
go
python
julia
allNames := []string{"Int8", "Int16", "Int32", "Int64", "Uint8", "Uint16", "Uint32"}
allTypes := [][]int{
    {math.MinInt8, math.MaxInt8}, {math.MinInt16, math.MaxInt16}, {math.MinInt32, math.MaxInt32}, 
    {math.MinInt64, math.MaxInt64}, {0, math.MaxUint8}, {0, math.MaxUint16}, {0, math.MaxUint32}}
for i, Type := range allTypes {
    fmt.Printf("%s: %v\n", allNames[i], Type)
}
// Int8: [-128 127]
// Int16: [-32768 32767]
// Int32: [-2147483648 2147483647]
// Int64: [-9223372036854775808 9223372036854775807]
// Uint8: [0 255]
// Uint16: [0 65535]
// Uint32: [0 4294967295]
# using PyTorch, the deep learning library
for int_type in [torch.int8, torch.int16, torch.int32, torch.int64, torch.uint8]:
    print(torch.iinfo(int_type))
# iinfo(min=-128, max=127, dtype=int8)
# iinfo(min=-32768, max=32767, dtype=int16)
# iinfo(min=-2.14748e+09, max=2.14748e+09, dtype=int32)
# iinfo(min=-9.22337e+18, max=9.22337e+18, dtype=int64)
# iinfo(min=0, max=255, dtype=uint8)
for T in [Int8,Int16,Int32,Int64,Int128,UInt8,UInt16,UInt32,UInt64,UInt128]
    println("$(lpad(T,7)): [$(typemin(T)),$(typemax(T))]")
end
# Int8: [-128,127]
# Int16: [-32768,32767]
# Int32: [-2147483648,2147483647]
# Int64: [-9223372036854775808,9223372036854775807]
# Int128: [-170141183460469231731687303715884105728,170141183460469231731687303715884105727]
# UInt8: [0,255]
# UInt16: [0,65535]
# UInt32: [0,4294967295]
# UInt64: [0,18446744073709551615]
# UInt128: [0,340282366920938463463374607431768211455]
Finishing of the summary of this chapter, the codes for the conversion between decimal, binary, octal and hexadecimal are as below
go
js
dart
python
julia
a := 10
b := fmt.Sprintf("%b", a) // dec to bin
d, _ := strconv.ParseInt(b, 2, 64) // import "strconv"
o := fmt.Sprintf("%o", a) // dec to octal
d, _ = strconv.ParseInt(o, 8, 64) // octal to decimal
h := fmt.Sprintf("%x", a) // decimal to hex
d, _ = strconv.ParseInt(h, 16, 64) // hex to decimal
let d // initialize null variable
let a = 10 // declare original decimal value
let b = a.toString(2) // dec to bin
d = parseInt(b, 2) // bin to dec
let o = a.toString(8) // dec to octal
d = parseInt(o, 8) // octal to decimal
let h = a.toString(16) // dec to hex
d = parseInt(h, 16) // hex to dec
var a = 10;
String b = a.toRadixString(2); // 1010
print(int.parse(b, radix:2)); 
String o = a.toRadixString(8); // 12
print(int.parse(o, radix:8)); 
String h = a.toRadixString(16); // a
print(int.parse(h, radix:16));
a = 10
b = bin(a) # dec to bin, 0b1010
d = int(b, 2) # bin to dec
o = oct(a) # dec to octal, 0o12
d = int(o, 8) # octal to dec
h = hex(a) # dec to hex, 0xa
d = int(h, 16) # hex to dec
a = 10
bin = string(a, base = 2) # dec to binary
dec = parse(Int, "0b$(bin)") # bin to dec
oc = string(a, base = 8) # dec to octal
dec = parse(Int, "0o$(oc)") # octal to dec
hex = string(a, base = 16) # dec to hexadecimal
dec = parse(Int, "0x$(hex)") # hex to decimal