Skip to content

Bitwise operators

Bit level operations on integers.

Every program can use these operators. No import is necessary.

Operator Arguments Gives
bit_and left: int, right: int int
bit_not value: int int
bit_or left: int, right: int int
bit_xor left: int, right: int int

Target support marks

  • Own template
  • Default template
  • No template

The operator works for a target with an own template and for a target with the default template. The default template is the * entry of the definition: it writes the same code for every target that has no template of its own. A target with no template writes no code, and a program that uses the operator does not compile for that target.

bit_and

(bit_and left right) → int
bit_and(left: int, right: int)

Gives the bitwise AND of the two integers.

A bit of the result is 1 when the same bit is 1 in both arguments.

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: default template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: default template
  • Scala: default template

Bit operations on an integer

docs/examples/core/bitwise/bitwise.rgr
class Main {
sfn main:void () {
def flags 12
def mask 10
print ("and " + (bit_and flags mask))
print ("or " + (bit_or flags mask))
print ("xor " + (bit_xor flags mask))
print ("not " + (bit_not flags))
}
}
main function, JavaScript
const flags = 12;
const mask = 10;
console.log("and " + (flags & mask));
console.log("or " + (flags | mask));
console.log("xor " + (flags ^ mask));
console.log("not " + (~flags));
The complete file
class Main {
constructor() {
}
}
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
const flags = 12;
const mask = 10;
console.log("and " + (flags & mask));
console.log("or " + (flags | mask));
console.log("xor " + (flags ^ mask));
console.log("not " + (~flags));
}
__js_main();

Definition: compiler/Lang.rgr, line 4628

bit_not

(bit_not value) → int
bit_not(value: int)

Bitwise NOT (ones complement)

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: default template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: default template
  • Scala: default template

Bit operations on an integer

docs/examples/core/bitwise/bitwise.rgr
class Main {
sfn main:void () {
def flags 12
def mask 10
print ("and " + (bit_and flags mask))
print ("or " + (bit_or flags mask))
print ("xor " + (bit_xor flags mask))
print ("not " + (bit_not flags))
}
}
main function, JavaScript
const flags = 12;
const mask = 10;
console.log("and " + (flags & mask));
console.log("or " + (flags | mask));
console.log("xor " + (flags ^ mask));
console.log("not " + (~flags));
The complete file
class Main {
constructor() {
}
}
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
const flags = 12;
const mask = 10;
console.log("and " + (flags & mask));
console.log("or " + (flags | mask));
console.log("xor " + (flags ^ mask));
console.log("not " + (~flags));
}
__js_main();

Definition: compiler/Lang.rgr, line 4678

bit_or

(bit_or left right) → int
bit_or(left: int, right: int)

Bitwise OR

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: default template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: default template
  • Scala: default template

Bit operations on an integer

docs/examples/core/bitwise/bitwise.rgr
class Main {
sfn main:void () {
def flags 12
def mask 10
print ("and " + (bit_and flags mask))
print ("or " + (bit_or flags mask))
print ("xor " + (bit_xor flags mask))
print ("not " + (bit_not flags))
}
}
main function, JavaScript
const flags = 12;
const mask = 10;
console.log("and " + (flags & mask));
console.log("or " + (flags | mask));
console.log("xor " + (flags ^ mask));
console.log("not " + (~flags));
The complete file
class Main {
constructor() {
}
}
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
const flags = 12;
const mask = 10;
console.log("and " + (flags & mask));
console.log("or " + (flags | mask));
console.log("xor " + (flags ^ mask));
console.log("not " + (~flags));
}
__js_main();

Definition: compiler/Lang.rgr, line 4646

bit_xor

(bit_xor left right) → int
bit_xor(left: int, right: int)

Bitwise XOR

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: default template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: default template
  • Scala: default template

Bit operations on an integer

docs/examples/core/bitwise/bitwise.rgr
class Main {
sfn main:void () {
def flags 12
def mask 10
print ("and " + (bit_and flags mask))
print ("or " + (bit_or flags mask))
print ("xor " + (bit_xor flags mask))
print ("not " + (bit_not flags))
}
}
main function, JavaScript
const flags = 12;
const mask = 10;
console.log("and " + (flags & mask));
console.log("or " + (flags | mask));
console.log("xor " + (flags ^ mask));
console.log("not " + (~flags));
The complete file
class Main {
constructor() {
}
}
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
const flags = 12;
const mask = 10;
console.log("and " + (flags & mask));
console.log("or " + (flags | mask));
console.log("xor " + (flags ^ mask));
console.log("not " + (~flags));
}
__js_main();

Definition: compiler/Lang.rgr, line 4662

Ranger 3.5.1 · commit f323fd4 · development build