Basic Operations
Table 1 describes the basic operations supported by the Hydra language.
Operator |
Description |
|---|---|
Integer operations: plus (+), minus (-), and bitwise NOT (~) |
Only integer types with a bit width of 32 or less are supported. int<32> a = 32w1; int<32> b; b = +a; // Plus b = -a; // Minus b = ~a; // Bitwise NOT |
Arithmetic operations: addition (+), subtraction (-), multiplication (*), division (/), and modulo (%) |
Only integer types with a bit width of 32 or less are supported. bit<32> a = 32w1; bit<32> b = 32w2; bit<32> c; c = a + b; // Addition c = a - b; // Subtraction c = a * b; // Multiplication c = a / b; // Division c = a % b // Modulo |
Shift operations: left shift (<<) and right shift (>>) |
Only integer types with a bit width of 32 or less are supported. bit<32> a = 32w1; bit<32> b = 32w2; bit<32> c; c = a << 2w2; // Left shift c = a >> 2w2; // Right shift |
Bitwise operations: bitwise AND (&), bitwise OR (|), and bitwise XOR (^) |
Only integer types with a bit width of 32 or less are supported. bit<32> a = 32w1; bit<32> b = 32w2; bit<32> c; c = a & b; // Bitwise AND c = a | b; // Bitwise OR c = a ^ b; // Bitwise XOR |
Comparison operations: less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), and not equal to (!=) |
Only integer types with a bit width of 32 or less are supported. The return value is of the Boolean type. bit<32> a = 32w1; bit<32> b = 32w2; bool d; d = a < b; // Less than d = a <= b; // Less than or equal to d = a > b; // Greater than d = a >= b; // Greater than or equal to d = a == b; // Equal to d = a != b; // Not equal to |
Logical operations: AND (&&), OR (||), and NOT (!) |
The variables involved in logical operations must be Boolean values. bool d; d = true && false; // AND d = true || false; // OR d = !true; // NOT |