Skip to main content

bitwise

Trait bitwise 

Source
pub trait bitwise {
    // Provided methods
    fn __band(&self, other: any) -> any { ... }
    fn __bor(&self, other: any) -> any { ... }
    fn __bxor(&self, other: any) -> any { ... }
    fn __bnot(&self) -> any { ... }
    fn __shl(&self, other: any) -> any { ... }
    fn __shr(&self, other: any) -> any { ... }
}
Expand description

Metamethods controlling the bitwise operators.

These events fire when a bitwise operator is applied to an operand that is neither an integer nor a value convertible to an integer.

Provided Methods§

Source

fn __band(&self, other: any) -> any

The bitwise AND (&) operation.

§Examples
local mt = { __band = function(a, b) return a.bits & b.bits end }
local a = setmetatable({ bits = 0x0F }, mt)
local b = setmetatable({ bits = 0x03 }, mt)
print(a & b)    --> 3
Source

fn __bor(&self, other: any) -> any

The bitwise OR (|) operation.

§Examples
mt.__bor = function(a, b) return a.bits | b.bits end
print(a | b)    --> 15
Source

fn __bxor(&self, other: any) -> any

The bitwise exclusive OR (~) operation.

§Examples
mt.__bxor = function(a, b) return a.bits ~ b.bits end
print(a ~ b)    --> 12
Source

fn __bnot(&self) -> any

The bitwise NOT (unary ~) operation.

§Examples
mt.__bnot = function(v) return ~v.bits & 0xFF end
print(~a)    --> 240
Source

fn __shl(&self, other: any) -> any

The bitwise left shift (<<) operation.

§Examples
mt.__shl = function(a, b) return a.bits << b.bits end
print(a << b)    --> 120
Source

fn __shr(&self, other: any) -> any

The bitwise right shift (>>) operation.

§Examples
mt.__shr = function(a, b) return a.bits >> b.bits end
print(a >> b)    --> 1

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§