In the C programming language, operations can be performed on a bit level using bitwise operators.
Strong and durable, these multipurpose bits are for use with bit screwdrivers. They are often used with power tools to install sheet metal screws. Professionals require commercial duty bits and fastening accessories that can endure the force of high-volume screw driving applications with high torque impact drivers. Standard screwdriving bits are brittle and fit poorly into fasteners, and in applications with high torque impact drivers this losing combo quickly leads to broken bits. Get Bitwise Today. Cracked Screen Fast, Professional and Guaranteed. Get your life back and repair your device. Support - Onsite or InOffice. BitWise AudioVisual BitWise AV Controllers are designed for customizable control of sub-systems within home theaters, retail, restaurants, boardrooms, classrooms, houses of worship, and much more. Integrators can create custom interfaces for OmniTouch 7, iOS, and Android devices, ensuring complete end-user satisfaction. The ^ (bitwise XOR) in C or C takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.
Bitwise operations are contrasted by byte-level operations which characterize the bitwise operators' logical counterparts, the AND, OR and NOT operators. Instead of performing on individual bits, byte-level operators perform on strings of eight bits (known as bytes) at a time. The reason for this is that a byte is normally the smallest unit of addressable memory (i.e. data with a unique memory address).
This applies to bitwise operators as well, which means that even though they operate on only one bit at a time they cannot accept anything smaller than a byte as their input.
All of these operators are also available in C++, and many C-family languages.
Bitwise operators[edit]
C provides six operators for bit manipulation.[1]
Symbol | Operator |
---|---|
& | bitwise AND |
| | bitwise inclusive OR |
^ | bitwise XOR (exclusive OR) |
<< | left shift |
>> | right shift |
~ | bitwise NOT (one's complement) (unary) |
Bitwise AND &
[edit]
bit a | bit b | a & b (a AND b) |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
The bitwise AND operator is a single ampersand: &
. It is just a representation of AND which does its work on the bits of the operands rather than the truth value of the operands. Bitwise binary AND does the logical AND (as shown in the table above) of the bits in each position of a number in its binary form.
For instance, working with a byte (the char type):
The most significant bit of the first number is 1 and that of the second number is also 1 so the most significant bit of the result is 1; in the second most significant bit, the bit of second number is zero, so we have the result as 0. [2]
Bitwise OR |
[edit]
bit a | bit b | a | b (a OR b) |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Similar to bitwise AND, bitwise OR only operates at the bit level. Its result is a 1 if either of the bits is 1 and zero only when both bits are 0. Its symbol is |
which can be called a pipe.
Bitwise XOR ^
[edit]
bit a | bit b | a ^ b (a XOR b) |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
The bitwise XOR (exclusive or) performs a logical XOR function, which is equivalent to adding two bits and discarding the carry. The result is zero only when we have two zeroes or two ones.[3] XOR can be used to toggle the bits between 1 and 0. Thus i = i ^ 1
when used in a loop toggles its values between 1 and 0.[4]
Bitwise NOT ~
/ ones' complement (unary)[edit]
The bitwise complement operator, the tilde, ~, flips every bit. A useful way to remember this is that the tilde is sometimes called a twiddle, and the bitwise complement twiddles every bit: if you have a 1, it's a 0, and if you have a 0, it's a 1.
This turns out to be a great way of finding the largest possible value for an unsigned number:
1 | unsigned int max = ~0; |
0, of course, is all 0s: 00000000 00000000. Once we twiddle 0, we get all 1s: 11111111 11111111. Since max is an unsigned int, we don't have to worry about sign bits or twos complement. We know that all 1s is the largest possible number.
Note that ~ and ! cannot be used interchangeably. When you take the logical NOT of a non-zero number, you get 0 (FALSE). However, when you twiddle a non-zero number, the only time you'll get 0 is when every bit is turned on. (This non-equivalence principle holds true for bitwise AND too, unless you know that you are using strictly the numbers 1 and 0. For bitwise OR, to be certain that it would be equivalent, you'd need to make sure that the underlying representation of 0 is all zeros to use it interchangeably. But don't do that! It'll make your code harder to understand.)
Now that we have a way of flipping bits, we can start thinking about how to turn off a single bit. We know that we want to leave other bits unaffected, but that if we have a 1 in the given position, we want it to be a 0. Action driver jobs. Take some time to think about how to do this before reading further.
We need to come up with a sequence of operations that leaves 1s and 0s in the non-target position unaffected; before, we used a bitwise OR, but we can also use a bitwise AND. 1 AND 1 is 1, and 0 AND 1 is 0. Now, to turn off a bit, we just need to AND it with 0: 1 AND 0 is 0. So if we want to indicate that car 2 is no longer in use, we want to take the bitwise AND of XXXXX1XX with 11111011.
How can we get that number? This is where the ability to take the complement of a number comes in handy: we already know how to turn a single bit on. If we turn one bit on and take the complement of the number, we get every bit on except that bit:
Bitwise Divide
1 | ~(1< |
Now that we have this, we can just take the bitwise AND of this with the current field of cars, and the only bit we'll change is the one of the car_num we're interested in.
1 2 3 4 | void set_unused(int car_num)
|
You might be thinking to yourself, but this is kind of clunky. We actually need to know whether a car is in use or not (if the bit is on or off) before we can know which function to call. While this isn't necessarily a bad thing, it means that we do need to know a little bit about what's going on. There is an easier way, but first we need the last bitwise operator: exclusive-or.
Shift operators[edit]
There are two bitwise shift operators. They are
- Right shift (
>>
) - Left shift (
<<
)
Right shift >>
[edit]
The symbol of right shift operator is >>
. For its operation, it requires two operands. It shifts each bit in its left operand to the right.The number following the operator decides the number of places the bits are shifted (i.e. the right operand).Thus by doing ch >> 3
all the bits will be shifted to the right by three places and so on.
Bitwise AND &
[edit]
bit a | bit b | a & b (a AND b) |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
The bitwise AND operator is a single ampersand: &
. It is just a representation of AND which does its work on the bits of the operands rather than the truth value of the operands. Bitwise binary AND does the logical AND (as shown in the table above) of the bits in each position of a number in its binary form.
For instance, working with a byte (the char type):
The most significant bit of the first number is 1 and that of the second number is also 1 so the most significant bit of the result is 1; in the second most significant bit, the bit of second number is zero, so we have the result as 0. [2]
Bitwise OR |
[edit]
bit a | bit b | a | b (a OR b) |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Similar to bitwise AND, bitwise OR only operates at the bit level. Its result is a 1 if either of the bits is 1 and zero only when both bits are 0. Its symbol is |
which can be called a pipe.
Bitwise XOR ^
[edit]
bit a | bit b | a ^ b (a XOR b) |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
The bitwise XOR (exclusive or) performs a logical XOR function, which is equivalent to adding two bits and discarding the carry. The result is zero only when we have two zeroes or two ones.[3] XOR can be used to toggle the bits between 1 and 0. Thus i = i ^ 1
when used in a loop toggles its values between 1 and 0.[4]
Bitwise NOT ~
/ ones' complement (unary)[edit]
The bitwise complement operator, the tilde, ~, flips every bit. A useful way to remember this is that the tilde is sometimes called a twiddle, and the bitwise complement twiddles every bit: if you have a 1, it's a 0, and if you have a 0, it's a 1.
This turns out to be a great way of finding the largest possible value for an unsigned number:
1 | unsigned int max = ~0; |
0, of course, is all 0s: 00000000 00000000. Once we twiddle 0, we get all 1s: 11111111 11111111. Since max is an unsigned int, we don't have to worry about sign bits or twos complement. We know that all 1s is the largest possible number.
Note that ~ and ! cannot be used interchangeably. When you take the logical NOT of a non-zero number, you get 0 (FALSE). However, when you twiddle a non-zero number, the only time you'll get 0 is when every bit is turned on. (This non-equivalence principle holds true for bitwise AND too, unless you know that you are using strictly the numbers 1 and 0. For bitwise OR, to be certain that it would be equivalent, you'd need to make sure that the underlying representation of 0 is all zeros to use it interchangeably. But don't do that! It'll make your code harder to understand.)
Now that we have a way of flipping bits, we can start thinking about how to turn off a single bit. We know that we want to leave other bits unaffected, but that if we have a 1 in the given position, we want it to be a 0. Action driver jobs. Take some time to think about how to do this before reading further.
We need to come up with a sequence of operations that leaves 1s and 0s in the non-target position unaffected; before, we used a bitwise OR, but we can also use a bitwise AND. 1 AND 1 is 1, and 0 AND 1 is 0. Now, to turn off a bit, we just need to AND it with 0: 1 AND 0 is 0. So if we want to indicate that car 2 is no longer in use, we want to take the bitwise AND of XXXXX1XX with 11111011.
How can we get that number? This is where the ability to take the complement of a number comes in handy: we already know how to turn a single bit on. If we turn one bit on and take the complement of the number, we get every bit on except that bit:
Bitwise Divide
1 | ~(1< |
Now that we have this, we can just take the bitwise AND of this with the current field of cars, and the only bit we'll change is the one of the car_num we're interested in.
1 2 3 4 | void set_unused(int car_num)
|
You might be thinking to yourself, but this is kind of clunky. We actually need to know whether a car is in use or not (if the bit is on or off) before we can know which function to call. While this isn't necessarily a bad thing, it means that we do need to know a little bit about what's going on. There is an easier way, but first we need the last bitwise operator: exclusive-or.
Shift operators[edit]
There are two bitwise shift operators. They are
- Right shift (
>>
) - Left shift (
<<
)
Right shift >>
[edit]
The symbol of right shift operator is >>
. For its operation, it requires two operands. It shifts each bit in its left operand to the right.The number following the operator decides the number of places the bits are shifted (i.e. the right operand).Thus by doing ch >> 3
all the bits will be shifted to the right by three places and so on.
However, do note that a shift operand value which is either a negative number or is greater than or equal to the total number of bits in this value results in undefined behavior. For example, when shifting a 32 bit unsigned integer, a shift amount of 32 or higher would be undefined.
Makita Impact Driver Bits
Example:
- If the variable
ch
contains the bit pattern11100101
, thench >> 1
will produce the result01110010
, andch >> 2
will produce00111001
.
Here blank spaces are generated simultaneously on the left when the bits are shifted to the right. When performed on an unsigned type or a non-negative value in an signed type, the operation performed is a logical shift, causing the blanks to be filled by 0
s (zeros). When performed on a negative value in a signed type, the result is technically implementation-defined (compiler dependent),[5] however most compilers will perform an arithmetic shift, causing the blank to be filled with the set sign bit of the left operand.
Right shift can be used to divide a bit pattern by 2 as shown:
Right shift operator usage[edit]
Typical usage of a right shift operator in C can be seen from the following code.
Example:
The output of the above program will be
Left shift <<
[edit]
The symbol of left shift operator is <<
. It shifts each bit in its left-hand operand to the left by the number of positions indicated by the right-hand operand. It works opposite to that of right shift operator. Thus by doing ch << 1
in the above example we have 11001010
.Blank spaces generated are filled up by zeroes as above.
However, do note that a shift operand value which is either a negative number or is greater than or equal to the total number of bits in this value results in undefined behavior. This is defined in the standard at ISO 9899:2011 6.5.7 Bit-wise shift operators.For example, when shifting a 32 bit unsigned integer, a shift amount of 32 or higher would be undefined.
Left shift can be used to multiply an integer by powers of 2 as in
Example: a simple addition program[edit]
The following program adds two operands using AND, XOR and left shift (<<).
Bitwise assignment operators[edit]
C provides a compound assignment operator for each binary arithmetic and bitwise operation (i.e. each operation which accepts two operands). Each of the compound bitwise assignment operators perform the appropriate binary operation and store the result in the left operand.[6]
The bitwise assignment operators are as follows:
Symbol | Operator |
---|---|
&= | bitwise AND assignment |
|= | bitwise inclusive OR assignment |
^= | bitwise exclusive OR assignment |
<<= | left shift assignment |
>>= | right shift assignment |
Logical equivalents[edit]
Four of the bitwise operators have equivalent logical operators. They are equivalent in that they have the same truth tables. However, logical operators treat each operand as having only one value, either true or false, rather than treating each bit of an operand as an independent value. Logical operators consider zero false and any nonzero value true. Another difference is that logical operators perform short-circuit evaluation.
The table below matches equivalent operators and shows a and b as operands of the operators.
Bitwise | Logical |
---|---|
a & b | a && b |
a | b | a || b |
a ^ b | a != b |
~a | !a |
!=
has the same truth table as ^
but unlike the true logical operators, by itself !=
is not strictly speaking a logical operator. This is because a logical operator must treat any nonzero value the same. To be used as a logical operator !=
requires that operands be normalized first. A logical not applied to both operands won't change the truth table that results but will ensure all nonzero values are converted to the same value before comparison. This works because !
on a zero always results in a one and !
on any nonzero value always results in a zero.
Example:
The output of the above program will be
See also[edit]
Nut Drivers Bits
References[edit]
- ^Kernighan; Dennis M. Ritchie (March 1988). The C Programming Language (2nd ed.). Englewood Cliffs, NJ: Prentice Hall. ISBN0-13-110362-8. Archived from the original on 2019-07-06. Retrieved 2019-09-07. Regarded by many to be the authoritative reference on C.
- ^ ab'Tutorials - Bitwise Operators and Bit Manipulations in C and C++'. cprogramming.com.
- ^'Exclusive-OR Gate Tutorial'. Basic Electronics Tutorials.
- ^'C++ Notes: Bitwise Operators'. fredosaurus.com.
- ^'ISO/IEC 9899:2011 - Information technology -- Programming languages -- C'. www.iso.org.
- ^'C/C++ Compound assignment operators'. XL C/C++ V8.0 for AIX. IBM. Retrieved 11 November 2013.
Square Driver Bits
External links[edit]
PHILLIPS SCREWDRIVER BITS | ||||
---|---|---|---|---|
DRIVER BIT | LENGTH | BLISTER QTY. | JAR QTY. | ITEM NO. |
#1 PH | 1' | 100 | 250 | BIP1100 |
#2 PH | 1' | 100 | 500 | BIP2100 |
#3 PH | 1' | 100 | 250 | BIP3100 |
#1 PH | 2' | 50 | 125 | BIP1200 |
#2 PH | 2' | 50 | 125 | BIP2200 |
#3 PH | 2' | 50 | 125 | BIP3200 |
#1 PH | 2-3/4' | 40 | 125 | BIP1275 |
#2 PH | 2-3/4' | 40 | 125 | BIP2275 |
#3 PH | 2-3/4' | 40 | 125 | BIP3275 |
#2 PH | 3-1/2' | 40 | 75 | BIP2350 |
#2 PH | 6' | 10* | N/A | BIP2600* |
*#2 PH 6' BLISTER PACK bits are packed in tubes. |
Bitwise Reverse Bits
DOUBLE ENDED SCREWDRIVER BITS | ||||
---|---|---|---|---|
#2 Phillips on one side and 8-10 slotted on the other. Overall length is 1-3/4'. | ||||
DRIVER BIT | LENGTH | BLISTER QTY. | JAR QTY. | ITEM NO. |
#2 PH / 8 – 10 SLOTTED | 1-3/4' | 50 | 125 | BIA175 |
SQUARE SCREWDRIVER BITS | ||||
---|---|---|---|---|
DRIVER BIT | LENGTH | BLISTER QTY. | JAR QTY. | ITEM NO. |
#1 SQ | 1' | 100 | 250 | BIQ1100 |
#2 SQ | 1' | 100 | 250 | BIQ2100 |
#3 SQ | 1' | 100 | 250 | BIQ3100 |
#1 SQ | 2' | 50 | 125 | BIQ1200 |
#2 SQ | 2' | 50 | 125 | BIQ2200 |
#1 SQ | 2-3/4' | 25 | 125 | BIQ1275 |
#2 SQ | 2-3/4' | 25 | 125 | BIQ2275 |
STAR SCREWDRIVER BITS | ||||
---|---|---|---|---|
DRIVER BIT | LENGTH | BLISTER QTY. | JAR QTY. | ITEM NO. |
T-10 Star | 1' | 25 | 250 | BIX10 |
T-15 Star | 1' | 25 | 250 | BIX15 |
T-20 Star | 1' | 25 | 250 | BIX20 |
T-20 Star | 2' | 25 | 125 | BIX20200 |
T-25 Star | 1' | 25 | 250 | BIX25 |
SOCKET DRIVER | |||
---|---|---|---|
SIZE | BLISTER QTY. | JAR QTY. | ITEM NO. |
1/4' x 3' | 25 | 50 | BIC25300 |
3/8' x 2-9/16' | 25 | 50 | BIC38256 |
SLOTTED SCREWDRIVER BITS | ||||
---|---|---|---|---|
DRIVER BIT | LENGTH | BLISTER QTY. | JAR QTY. | ITEM NO. |
6-8 SL | 1' | 25 | 250 | BIS06100 |
8-10 SL | 1' | 25 | 250 | BIS08100 |
10-12 SL | 1' | 25 | 250 | BIS10100 |
6-7 SL | 2-3/4' | 25 | 125 | BIS06275 |
8-10 SL | 2-3/4' | 25 | 125 | BIS08275 |
10-12 SL | 2-3/4' | 25 | 125 | BIS10275 |
MAGNETIC HEX NUT SETTER | |||
---|---|---|---|
SIZE | BLISTER QTY. | JAR QTY. | ITEM NO. |
1/4' x 1-5/8' | 25 | 40 | BIH25162 |
5/16' x 1-5/8' | 25 | 40 | BIH31162 |
3/8' x 1-7/8' | 15 | 40 | BIH38188 |
7/16' x 2' | 15 | 40 | BIH44200 |
1/2' x 2' | 15 | 40 | BIH50200 |
1/4' x 2-9/16' | 15 | 40 | BIH25256 |
5/16' x 2-9/16' | 15 | 40 | BIH31256 |
3/8' x 2-9/16' | 15 | 40 | BIH38256 |
DRIVER: ACOUSTICAL EYE-LAG SCREW | ||
---|---|---|
BLISTER QTY. | JAR QTY. | ITEM NO. |
10 | 40 | BIA170 |
DIMPLER | #2 Phillips | ||
---|---|---|
BLISTER QTY. | JAR QTY. | ITEM NO. |
50 | 125 | BIA150 |