Bitwise data manipulation is working with data at the bit level, instead of working with bytes or larger units of data, as is more common. A bitwise operator (a character that represents an action, as for example x is an arithmetic operator that represents multiplication) manipulates single bits. In comparison, most operators work with either single or multiple bytes. Not all programming languages support the use of bitwise operators. C, JavaScript, and Visual Basic are among those that do. A bitwise operator works with the binary representation of a number rather than that number's value. The operand is treated as a set of bits, instead of as a single number. Bitwise operators are similar in most languages that support them.
Next Steps
-
Understanding ABAP programming bits and bytes
In this book chapter excerpt, find out how to r...
(SearchSAP.com) -
A bit wiser with Oracle technology
Learn how to use a metadata repository to ident...
(SearchOracle.com)
Because they allow greater precision, bitwise operators can make some code faster and more efficient. The following example, borrowed from Charity Kahn's article on CNET Builder.com (8/26/98), compares two versions of JavaScript code used to map several form checkboxes to a single number:
var combo = form.elements[0].checked*(Math.pow(2,2))
+ form.elements[1].checked*(Math.pow(2,1))
+ form.elements[2].checked*(Math.pow(2,0));
Bitwise operators can be used to do the same thing, but more efficiently:
var combo = form.elements[0].checked << 2
| form.elements[1].checked << 1
| form.elements[2].checked << 0
JavaScript Bitwise Operators
| Operator | Name | Type | Action |
| & | Bitwise AND | binary | If bits of both operands are ones, returns a one in each bit position |
| | | Bitwise OR | binary | If bits of either operand are ones, returns a one in a bit position |
| ^ | Bitwise XOR | binary | If a single operand is a one, returns a one in a bit position |
| ~ | Bitwise NOT | unary | Flips the bits in the operand |
| << | Left shift | binary | Shifts first operand a number of bits to the left as specified in the second operand, shifting in zeroes from the right |
| >> | Right shift | binary | Shifts first operand a number of bits to the right as specified in the second operand, and discards displaced bits |
| >>> | Zero-fill right shift | binary | Shifts first operand a number of bits to the right as specified in the second operand, discards displaced bits, and shifts in zeroes from the left |
Tech TalkComment
Share
Comments
Results
Contribute to the conversation