5 stars based on
The bitwise and bit-shift operators are not covered in CS in great detail, but they are included here for completeness and to provide a consistent location for later reference and review. Unless this section is explicitly assigned by your instructor, you may skip it for now and return to those parts that are relevant later. Bit-manipulation operations are always available in assembly language but are less common in higher-level binaroperationen mit 8-bit. Including these operators in C made it possible to write operating systems and device drivers in C rather than in assembly.
All of the bit-manipulation operators require two integer arguments, which we will often view in binary for convenience. It is further convenient for us to divide the operators into two groups: Each of the four bitwise operators operate on the corresponding bits of the two operands. The result of combining two bits with a bitwise operator is a single bit, so the easiest way to describe the effect of each bit-by-bit operation is with a truth table.
The following figures detail. In practice, one operand is often an integer variable and the other is an integer binaroperationen mit 8-bit. A single hexadecimal digit corresponds to a nibble i. One of the most common uses for bitwise binaroperationen mit 8-bit is to set, reset, and test bits stored in a multi-bit data structure.
A "multi-bit data structure" may be as simple as an integer, which may hold anywhere from 8 to 64 bits. Arrays chapter 7 of integers or similar data structures may also be used.
Multi-bit data are often called bit fields, bit sets, bit vectors, bit maps, bit strings, etc. Bit fields are often used to compactly binaroperationen mit 8-bit a wide variety of settings binaroperationen mit 8-bit conditions. Each bit in the bit field is treated like a switch: A "1" indicates that a setting is on while a "0" indicates that the setting is off.
Bitwise OR and bitwise AND operators are binaroperationen mit 8-bit in conjunction with constant bit fields, called bit masksto switch bits on and off respectively, which corresponds to switching settings on or off. The characteristics are governed by a bit field whose bits correspond to specific behaviors; programmers set the bits to configure a file to behave in a desired way.
Programmers use named bit masks i. To create a value that will switch on all four behaviors, a programmer might write an assignment statement such as:.
The bitwise OR operations produce the bit field: The bitwise AND operation switches off all of the bits except the one corresponding to the binary bit mask. The two bit-shift operators should look familiar to you, not because we have binaroperationen mit 8-bit them before, but because they are reused as the output and input operators introduced previously. Both operands are integers and we will continue to view the left hand operand in binary but will now view the right hand operand in decimal.
Both bit-shift operators treat the left hand operand as a string of 1's and 0's that are shifted either left or right by the number of places indicated by binaroperationen mit 8-bit right hand operand.
This may sound confusing but is mostly easy to understand when illustrated by an example. The number of places that the bits are moved or shifted is specified by the right hand operand.
The right shift operator moves the bits to the right as expected and the number of binaroperationen mit 8-bit that the bits are move or shifted is still specified by the right hand operand. The bits shifted out, on binaroperationen mit 8-bit right now, are still discarded. But it is how the empty spaces, on the left now, are filled that makes the right shift operator more complicated than the left shift operator. Without programmer intervention it is the underlying hardware that determines how the spaces vacated by the shift are filled.
The ANSI standard calls such features implementation dependent. Some hardware implements sign extension i. Fortunately, programmers can intervene. The highest-order bit in a signed integer, the sign bit, denotes a negative value when it is 1 and a non-negative value when it is 0. A negative value is generally not needed when an integer stores a bit pattern and so the easy "fix" is to define the integer as unsigned.
It's quite binaroperationen mit 8-bit to use unsigned integers, variables and constants with all of the bit-manipulation operators. When the right shift operator's left operand is unsigned, then the empty spaces on the left are always filled with 0's regardless of how the hardware behaves by default.