bitwise
Perform bitwise operations on numbers
TLDR
Run using interactive mode
Convert from decimal
Convert from hexadecimal
Convert a C-style calculation
SYNOPSIS
There is no direct 'bitwise' command with a standard synopsis. Instead, bitwise operations are performed using built-in shell features or other utilities.
Examples:
Bash/Zsh Arithmetic Expansion:
(( result = value1 & value2 ))
(( result = value1 | value2 ))
(( result = value1 << bits ))
expr command:
expr value1 \& value2 (Note: '&' needs escaping)
expr value1 \| value2 (Note: '|' needs escaping)
awk:
awk 'BEGIN { print (value1 & value2) }'
DESCRIPTION
The term 'bitwise' refers to operations that manipulate individual bits of binary numbers.
Unlike a dedicated 'bitwise' command, Linux shells and utilities perform bitwise operations through various built-in features, arithmetic expansions, or scripting languages. These operations are fundamental in low-level programming, network protocols, file permissions, and flag manipulation.
Common bitwise operators include AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). Users typically employ shell arithmetic (e.g., (( ... )) in Bash/Zsh), the expr command, awk, or higher-level scripting languages like Python or Perl to achieve bitwise logic within a Linux environment.
CAVEATS
The most significant caveat is that 'bitwise' is not a dedicated command in standard Linux distributions. Users often mistakenly search for such a command when they intend to perform bitwise operations. These operations must be executed within the context of a shell's arithmetic capabilities, a programming language, or specific utilities like expr or awk that support them. Shell arithmetic ((( ... )) in Bash/Zsh) is generally the most convenient and performant method for simple bitwise operations directly within scripts.
COMMON BITWISE OPERATORS
Understanding the basic bitwise operators is crucial:
Bitwise AND ( & ): Sets a bit if it is set in both operands.
Bitwise OR ( | ): Sets a bit if it is set in at least one of the operands.
Bitwise XOR ( ^ ): Sets a bit if it is set in only one of the operands.
Bitwise NOT ( ~ ): Inverts all bits (one's complement).
Left Shift ( << ): Shifts bits to the left, filling new bits with zeros. Equivalent to multiplying by powers of 2.
Right Shift ( >> ): Shifts bits to the right. For unsigned integers, new bits are zeros. For signed integers, behavior can be arithmetic (sign-extending) or logical (zero-filling).
HISTORY
The concept of bitwise operations is fundamental to computer science and programming, dating back to the earliest days of digital computing. These operations are intrinsic to how CPUs process data at the lowest level. As such, their availability in various forms (assembly instructions, high-level language operators, shell built-ins) has been a constant feature across all operating systems, including Unix and Linux. There isn't a specific 'bitwise' command with a development history, but rather a continuous evolution of how these operations are exposed and utilized within programming constructs and scripting environments.