How to Use Bitwise Operations in Low-Level Programming

In addition to the familiar arithmetic operations +, -, * and /, programming languages also support bitwise operations. Through these low-level operations, a programmer can access individual bits in a binary pattern regardless of the data type. Bitwise operations implement logical arithmetic and bit shifts and can provide much better efficiency than high-level operations.

Instructions

    • 1

      Place the NOT operator in front of a binary value to perform a bitwise NOT. This so-called one's complement operation reverses each bit value. In the C programming language, use the ~ symbol to indicate a NOT operation. For instance, if X is 0101, then ~X is 1010.

    • 2

      Perform logical arithmetic on binary values of equal length. Computers perform logical operations on each pair of bits going from right to left. In C, the symbols &, | and ^ indicate bitwise AND, OR and XOR operations, respectively.

    • 3

      Shift a bit pattern to the left or right, discarding bits that you shift off either end, to implement optimized multiplication (left shift) or division (right shift) by powers of two. The C language uses the << and >> operators for left and right bit shifting.

    • 4

      Determine the value of a particular bit in a binary number (a read-flag operation) by performing an AND operation. Create a new binary number (T, for instance) of the same length as the number you wish to examine (K, for instance). Set all the bits in T to zero, with the exception of bit N, where bit N in K is the bit that you are testing. After performing T AND K, the result will be zero if bit N in K is zero, or it will be non-zero if bit N in K is one.

    • 5

      Set a particular bit in a binary value to one (a write-flag operation). You can perform this operation in a manner similar to that of Step 4. If you want to set bit N of K to one, then set bit N in T to one and all other bits in T to zero. Perform the operation T OR K. The resulting number is identical to T, with the exception that bit N of T is one (regardless of whether it was previously one or zero).

Tips & Warnings

  • Don't confuse the bitwise operations AND, OR and XOR with their Boolean counterparts. For instance, C uses & for a bitwise AND and && for a Boolean AND.

Related Searches:

Comments

You May Also Like

  • What Causes Low Creatinine Levels?

    Serum creatinine is a laboratory test that is performed as part of routine blood work and to facilitate diagnosis of kidney disease,...

  • How to Perform a Bit Shift in C++

    Programmers can't directly access bits in C++, but C++ does offer the use of bit shift operators for performing certain operations on...

  • How to Use Assignment Operators in Java

    Java has a number of assignment operators, or statements which adjust the value of a variable. Besides the simple assignment and initialization...

  • How to Perform a Bit Shift in Java

    The Java programming language allows you to perform bit shift and bitwise operations on the binary numbers that underlie the various data...

  • How to Convert LBF to MPA

    Lesser Bitwise Flow (LBF) packets can be converted to Major Passic Arrangement (MPA) packets prior to Open Shortest Path First (OSPF) routing,...

  • How to Install a Jack Post

    Jack posts are designed to provide adjustable leveling of floors and beams, both in new and re-modeling situations. Some are one piece...

Related Ads

Featured