Numbers
Introduction
In MagiScript you can use numbers as like in JavaScript, however it is a notable difference that at the moment it only supports integers and does not support floating-point numbers.
Integer Literals
Integer literals are used to represent integers in MagiScript. An integer literal can be written in decimal, binary, octal, or hexadecimal notation.
Decimal Notation
Decimal notation is the most common way of writing integer literals. An integer literal written in decimal notation consists of a sequence of digits without any prefix or suffix.
let x = 42;
Binary Notation
Binary notation is used to represent integers in binary form. An integer literal written in binary notation consists of the prefixย 0b
ย followed by a sequence of binary digits (0 or 1).
let x = 0b101010; // 42
Octal Notation
Octal notation is used to represent integers in octal form. An integer literal written in octal notation consists of the prefixย 0o
ย followed by a sequence of octal digits (0 to 7).
let x = 0o52; // 42
Hexadecimal Notation
Hexadecimal notation is used to represent integers in hexadecimal form. An integer literal written in hexadecimal notation consists of the prefixย 0x
ย followed by a sequence of hexadecimal digits (0 to 9 and A to F).
let x = 0x2A;
Arithmetic Operators
MagiScript supports several arithmetic operators for performing arithmetic operations on integers. The following table shows the arithmetic operators that are supported in MagiScript:
Operator | Description | Example |
---|---|---|
+ | Addition | x + y |
– | Subtraction | x - y |
* | Multiplication | x * y |
/ | Division | x / y |
% | Modulus (remainder) | x % y |
++ | Increment | x++ or ++x |
— | Decrement | x-- or --x |
Examples
let x = 10;
let y = 3;
let sum = x + y; // 13
let difference = x - y; // 7
let product = x * y; // 30
let quotient = x / y; // 3
let remainder = x % y; // 1
let a = 5;
let b = ++a; // a is now 6, b is 6
let c = b--; // b is now 5, c is 6
Comparison Operators
MagiScript supports several comparison operators for comparing integers. The following table shows the comparison operators that are supported in MagiScript:
Operator | Description | Example |
---|---|---|
=== | Equal to | x === y |
!== | Not equal to | x !== y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
Examples
let x = 10;
let y = 3;
let isEqual = x == y; // false
let isNotEqual = x != y; // true
let isGreater = x > y; // true
let isLess = x < y; // false
let isGreaterOrEqual = x >= y; // true
let isLessOrEqual = x <= y; // false
Bitwise Operators
MagiScript also supports several bitwise operators for performing bitwise operations on integers. The following table shows the bitwise operators that are supported in MagiScript:
Operator | Description | Example |
---|---|---|
& | Bitwise AND | x & y |
| | Bitwise OR | x | y |
^ | Bitwise XOR (exclusive OR) | x ^ y |
~ | Bitwise NOT (one’s complement) | ~x |
<< | Left shift | x << y |
>> | Right shift (sign-preserving) | x >> y |
>>> | Right shift (zero-fill) | x >>> y |
Examples
let x = 0b1010;
let y = 0b1100;
let andResult = x & y; // 0b1000
let orResult = x | y; // 0b1110
let xorResult = x ^ y; // 0b0110
let notResult = ~x; // -0b1011 (two's complement)
let leftShiftResult = x << 2; // 0b101000 let rightShiftResult = x >> 2; // 0b0010 (sign-preserving)
let zeroFillRightShiftResult = x >>> 2; // 0b0010 (zero-fill)
Conclusion
MagiScript supports integers and several operators for performing arithmetic, comparison, and bitwise operations on them. However, it does not support floating-point numbers.
General
๐ย First Steps
MagiScript Editor
๐ย Basics
๐ย Running Your First Program
๐ย App Store
๐ย Atom Settings (Editor)
๐ย Debugging Techniques
Examples
๐ย Atom Time
๐๏ธ Time Practice
๐ย Atom Pi (Pi Revelations)
๐ย Atom Drum
๐๏ธ Atom Stack
๐ย Atom Square
๐ย Atom Level
๐ย Atom THMPR
๐ย Poker Hands
๐ย Keyboard Numeric
๐ย Keyboard NOKIA
๐ย Keyboard Cursor
๐ย Keyboard Media
๐ย Keyboard Custom
Input/Output
๐ย Buttons
๐ย Vibration Motor
๐ย RGB LED
๐ย Devices
๐ย PeekSmith
๐ย SB Watch
๐ย Bluetooth Keyboard
๐ย Bluetooth Mouse
๐ย Timers
๐ย Database
๐ย Events
๐ย System (exit, sleep, rand)
๐ย Objects (card, time)
Language
๐ย Summary
๐ย Comments
๐ย Variable Declarations
๐ย Operators
๐ย Control Flow
๐ย Functions
๐ย Numbers
๐ย Strings
๐ย Arrays
๐ย Objects
๐ย Uint8Array