The content on this page is written in Chinese, and then traslated into English by machine. More accurate traslations are welcome at: https://github.com/wa-lang/man/tree/master/en

Ending's law: "Any application that can be compiled to WebAssembly, will be compiled to WebAssembly eventually."

3.2. Integers

Wa-lang currently supports the following integer types:

  • u8: unsigned 8-bit integer;
  • u16: unsigned 16-bit integer;
  • i32: signed 32-bit integer;
  • u32: unsigned 32-bit integer;
  • i64: signed 64-bit integer;
  • u64: unsigned 64-bit integer;
  • int: signed integer;
  • uint: unsigned integer;
  • bool: Boolean type.

Among them:

  • int and uint are variable-width integers, and their width is determined by the target platform. The reason why there are variable-width integer types is that the addressing range of the target platform may be different. Operations involving the storage range such as the built-in function len require a unified data type to ensure that the code can be compiled normally on different target platforms. And make full use of the platform addressing range;
  • The actual memory layout of bool type is u8, the literal values of legal values are true, false, and the corresponding memory values ​​are 1 and 0.

The current main target platform of Wa-lang is wasm32. Under this platform, the bit width of variable-width integers is 32 bits, which is 4 bytes.

Integers other than Boolean support the following unary operations:

  • ^: bitwise negation
  • -: Take the negative arithmetic value (that is, subtract the operand from 0)

For example:

    i: u8 = 9
    println(^i) // 246
    println(-i) // 247

    j: i32 = 9
    println(^i) // -10
    println(-i) // -9

Integers other than Boolean support the following binary arithmetic operations:

  • +: Add, the two operand types must be consistent, the type of return value is same with operands;
  • -: Minus, the two operand types must be consistent, the type of return value is same with operands;
  • *: Mul, the two operand types must be consistent, the type of return value is same with operands;
  • /: Div, the two operand types must be consistent, the type of return value is same with operands;
  • %: Rem, the two operand types must be consistent, the type of return value is same with operands;

For example:

    i, j: u8 = 9, 250
    println(i + j) // 3
    println(i - j) // 15
    println(i * j) // 202
    println(j / i) // 27
    println(j % i) // 7

Integers(except Boolean) support the following binary bit operations:

  • &: bitwise AND, the two operand types must be consistent, the type of return value is same with operands;
  • |: Bitwise OR, the two operand types must be consistent, the type of return value is same with operands;
  • ^: Bitwise XOR, the two operand types must be consistent, the type of return value is same with operands;
  • &^: clear bitwise, the two operand types must be consistent, the type of return value is same with operands. For z = x &^ y, let xn, yn, zn be the nth bit of x, y, z respectively, then when yn is 1, zn is 0, otherwise zn is equal to xn. This operation is equivalent to z = x & (^y);
  • <<: Left shift, for z = x << y, the type of z is consistent with x, y must be an integer greater than 0, and the low-order bit is filled with 0 when shifting;
  • >>: Right shift, for z = x >> y, the type of z is consistent with x, y must be an integer greater than 0, and the high bit is filled with 0 when shifting.

For example:

    i, j: u16 = 343, 47831
    println(i & j)  // 87
    println(i | j)  // 48087
    println(i ^ j)  // 48000
    println(i &^ j) // 256
    println(i << 5) // 10976
    println(j >> 5) // 1494

The results of operations such as addition, subtraction, multiplication, and left shift may exceed the expression range of the operands. In this case, the low-order part will be intercepted as the result.

Integers except Boolean support the following comparison operations:

  • ==: equal. The operands types must be consistent, and the return value is of bool type. If the judgment condition is met, true will be returned, otherwise false will be returned, the same below;
  • !=: not equal;
  • >: greater than;
  • >=: greater than;
  • <: less than;
  • <=: Small equal to.

If one of the two operands involved in the comparison is a constant, the constant should be on the right side of the comparison operator.

Boolean types support the following unary operations:

  • !: negation, if the operand is false, return true, otherwise return false.

In fact, in addition to the named constants declared through constants introduced in Section 2.4, many literal values appearing in the code are also constants, such as:

     i := 13

13 in the code is an untyped integer constant. When using untyped integer constants for variable short-declaration, the type of the variable is a variable-width signed integer (that is, int). The above code is equivalent to:

     i: int
     i=13

When assigning an integer constant to an integer variable, type and range checking will be performed at compile time and automatically matched to the variable type - assigning a negative constant to an unsigned integer, or a constant value that exceeds the width of the assigned variable will be judged as illegal.

Integers support all binary operators, and binary operators have decreasing precedence in the following order (same precedence within the same line, executed from left to right):

*      /      %      <<       >>     &       &^
+      -      |      ^
==     !=     <      <=       >      >=
&&
||