Path: blob/main/crates/polars-arrow/src/compute/bitwise.rs
6939 views
//! Contains bitwise operators: [`or`], [`and`], [`xor`] and [`not`].1use std::ops::{BitAnd, BitOr, BitXor, Not};23use crate::array::PrimitiveArray;4use crate::compute::arity::{binary, unary};5use crate::types::NativeType;67/// Performs `OR` operation on two [`PrimitiveArray`]s.8/// # Panic9/// This function errors when the arrays have different lengths.10pub fn or<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>11where12T: NativeType + BitOr<Output = T>,13{14binary(lhs, rhs, lhs.dtype().clone(), |a, b| a | b)15}1617/// Performs `XOR` operation between two [`PrimitiveArray`]s.18/// # Panic19/// This function errors when the arrays have different lengths.20pub fn xor<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>21where22T: NativeType + BitXor<Output = T>,23{24binary(lhs, rhs, lhs.dtype().clone(), |a, b| a ^ b)25}2627/// Performs `AND` operation on two [`PrimitiveArray`]s.28/// # Panic29/// This function panics when the arrays have different lengths.30pub fn and<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>31where32T: NativeType + BitAnd<Output = T>,33{34binary(lhs, rhs, lhs.dtype().clone(), |a, b| a & b)35}3637/// Returns a new [`PrimitiveArray`] with the bitwise `not`.38pub fn not<T>(array: &PrimitiveArray<T>) -> PrimitiveArray<T>39where40T: NativeType + Not<Output = T>,41{42let op = move |a: T| !a;43unary(array, op, array.dtype().clone())44}4546/// Performs `OR` operation between a [`PrimitiveArray`] and scalar.47/// # Panic48/// This function errors when the arrays have different lengths.49pub fn or_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> PrimitiveArray<T>50where51T: NativeType + BitOr<Output = T>,52{53unary(lhs, |a| a | *rhs, lhs.dtype().clone())54}5556/// Performs `XOR` operation between a [`PrimitiveArray`] and scalar.57/// # Panic58/// This function errors when the arrays have different lengths.59pub fn xor_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> PrimitiveArray<T>60where61T: NativeType + BitXor<Output = T>,62{63unary(lhs, |a| a ^ *rhs, lhs.dtype().clone())64}6566/// Performs `AND` operation between a [`PrimitiveArray`] and scalar.67/// # Panic68/// This function panics when the arrays have different lengths.69pub fn and_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> PrimitiveArray<T>70where71T: NativeType + BitAnd<Output = T>,72{73unary(lhs, |a| a & *rhs, lhs.dtype().clone())74}757677