kc3-lang/kc3/doc/1 KC3/1.2 Integer.en.md

Download

2 Integers

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.

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

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.

ikc3> type(1000000000000000000000000000000)
Integer

2.3 Operations on integers

2.3.1 Operator ~

Binary not.

ikc3> ~ 1
254
ikc3> ~ -1
0
ikc3> ~ 0
255
ikc3> ~ 255
0
ikc3> ~ (U16) 1
65534
ikc3> ~ (S16) 1
-2

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.

1 = 0 + 1
2 = 0 + 1 + 1
3 = 0 + 1 + 1 + 1
etc.

2.3.3 Operator -

Integer subtraction. Take an integer and remove another integer from it.

2.3.4 Operator *

Integer multiplication. Init result to zero, add an integer a and repeat b times.

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.

2.3.6 Operator mod

Integer modulo. Returns r in the previous equation (see Operator /)

2.3.7 Operator <<

Left shift

2.4 Examples

ikc3> type(1)
U8
ikc3> type(1000000000000000000000000000000)
Integer
ikc3> a = 1 + 100000000000000000000000000000000
100000000000000000000000000000001
ikc3> a * a
10000000000000000000000000000000200000000000000000000000000000001
ikc3> a * a / 1000000000000000000000000000000000000000000000000000
10000000000000

Source

Download