Hash :
ef195ae9
Author :
Thomas de Grivel
Date :
2024-10-14T17:14:30
IKC3 supports all C integer type sizes from U8
(matching the C type uint8_t
)
to U64
(matching the C type uint64_t
) for unsigned integers,
and from S8
to S64
for signed integers.
In addition to these 8 basic integer types there are 2 pointer-sized
integer types :
Uw
for unsigned pointer-sized integers,
and Sw
for signed pointer-sized integers.
Small integers take little space, are static, they are passed directly and not as a pointer, and for these reasons are fast.
They do not need to be cleaned after use and thus can be used in arrays like is usually done in C.
ikc3> type(-1)
S8
ikc3> type(-128)
S16
ikc3> type(-32768)
S32
ikc3> type(-2147483648)
S64
ikc3> type(-9223372036854775807)
S64
ikc3> type(0)
U8
ikc3> type(256)
U16
ikc3> type(65536)
U32
ikc3> type(4294967296)
U64
ikc3> type(18446744073709551615)
U64
IKC3 supports large integers and they are compatible with small integers.
Their type is Integer
and can support numbers as large as memory allows.
They are slow because they are allocated dynamically on the heap
using malloc.
ikc3> type(1000000000000000000000000000000)
Integer
~
Binary not.
ikc3> ~ 1
254
ikc3> ~ -1
0
ikc3> ~ 0
255
ikc3> ~ 255
0
ikc3> ~ (U16) 1
65534
ikc3> ~ (S16) 1
-2
+
Integer addition.
All positive integers can be defined in terms of addition of : zero or a positive integer, and one. E.g.
1 = 0 + 1
2 = 0 + 1 + 1
3 = 0 + 1 + 1 + 1
etc.
-
Integer subtraction. Take an integer and remove another integer from it.
*
Integer multiplication. Init result to zero, add an integer a
and
repeat b
times.
/
Integer division. The inverse of multiplication :
for all integers a
and b
there is a couple q
and r
that satisfies
a = b * q + r
. Integer division returns q
.
mod
Integer modulo. Returns r
in the previous equation (see Operator /
)
<<
Left shift
ikc3> type(1)
U8
ikc3> type(1000000000000000000000000000000)
Integer
ikc3> a = 1 + 100000000000000000000000000000000
100000000000000000000000000000001
ikc3> a * a
10000000000000000000000000000000200000000000000000000000000000001
ikc3> a * a / 1000000000000000000000000000000000000000000000000000
10000000000000
Top : KC3 documentation
Previous : 1.1 Introduction
Next : 1.3 Map
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
# 1.2 Integer
## 1.2.1 Small integers
IKC3 supports all C integer type sizes from `U8` (matching the C type `uint8_t`)
to `U64` (matching the C type `uint64_t`) for unsigned integers,
and from `S8` to `S64` for signed integers.
In addition to these 8 basic integer types there are 2 pointer-sized
integer types :
`Uw` for unsigned pointer-sized integers,
and `Sw` for signed pointer-sized integers.
Small integers take little space, are static, they are passed directly and
not as a pointer, and for these reasons are fast.
They do not need to be cleaned after use and thus can be used in arrays like
is usually done in C.
```elixir
ikc3> type(-1)
S8
ikc3> type(-128)
S16
ikc3> type(-32768)
S32
ikc3> type(-2147483648)
S64
ikc3> type(-9223372036854775807)
S64
ikc3> type(0)
U8
ikc3> type(256)
U16
ikc3> type(65536)
U32
ikc3> type(4294967296)
U64
ikc3> type(18446744073709551615)
U64
```
## 1.2.2 Large integers
IKC3 supports large integers and they are compatible with small integers.
Their type is `Integer` and can support numbers as large as memory allows.
They are slow because they are allocated dynamically on the heap
using malloc.
```elixir
ikc3> type(1000000000000000000000000000000)
Integer
```
## 1.2.3 Operations on integers
### 1.2.3.1 Operator `~`
Binary not.
```elixir
ikc3> ~ 1
254
ikc3> ~ -1
0
ikc3> ~ 0
255
ikc3> ~ 255
0
ikc3> ~ (U16) 1
65534
ikc3> ~ (S16) 1
-2
```
### 1.2.3.2 Operator `+`
Integer addition.
All positive integers can be defined in terms of addition of :
zero or a positive integer, and one. E.g.
```elixir
1 = 0 + 1
2 = 0 + 1 + 1
3 = 0 + 1 + 1 + 1
etc.
```
### 1.2.3.3 Operator `-`
Integer subtraction. Take an integer and remove another integer from it.
### 1.2.3.4 Operator `*`
Integer multiplication. Init result to zero, add an integer `a` and
repeat `b` times.
### 1.2.3.5 Operator `/`
Integer division. The inverse of multiplication :
for all integers `a` and `b` there is a couple `q` and `r` that satisfies
`a = b * q + r`. Integer division returns `q`.
### 1.2.3.6 Operator `mod`
Integer modulo. Returns `r` in the previous equation (see Operator `/`)
### 1.2.3.7 Operator `<<`
Left shift
## 1.2.4 Examples
```elixir
ikc3> type(1)
U8
ikc3> type(1000000000000000000000000000000)
Integer
ikc3> a = 1 + 100000000000000000000000000000000
100000000000000000000000000000001
ikc3> a * a
10000000000000000000000000000000200000000000000000000000000000001
ikc3> a * a / 1000000000000000000000000000000000000000000000000000
10000000000000
```
---
Top : [KC3 documentation](/doc/)
Previous : [1.1 Introduction](1.1_Introduction)
Next : [1.3 Map](1.3_Map)