ArticleslgStudy

science

Bitwise operations in C

Bitwise operations in C is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Bitwise operations in C rather than just read about it. In short: In the C programming language, operations can be performed on a bit level using bitwise operators. Bitwise operations are contrasted by byte-level operations which characterize the bitwise operators' logical counterparts, the AND, OR, NOT operators.

Key takeaways

  • Bitwise operations in C belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Bitwise operations in C to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Bitwise operations in C from memory before moving on to harder problems.

Reference excerpt

In the C programming language, operations can be performed on a bit level using bitwise operators. Bitwise operations are contrasted by byte-level operations which characterize the bitwise operators' logical counterparts, the AND, OR, 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 C provides six operators for bit manipulation.

Bitwise AND &

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 performs logical conjunction (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):

11001000 & 10111000 -------- = 10001000

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.

Bitwise OR |

Similar to bitwise AND, bitwise OR performs logical disjunction 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 ^

The bitwise XOR (exclusive or) performs an exclusive disjunction, which is equivalent to adding two bits and discarding the carry. The result is zero only when we have two zeroes or two ones. 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.

Shift operators There are two bitwise shift operators. They are

Right shift (>>) Left shift (<<)

Right shift >> 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. Example:

If the variable ch contains the bit pattern 11100101, then ch >> 1 will produce the result 01110010, and ch >> 2 will produce 00111001. 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 a signed type, the operation performed is a logical shift, causing the blanks to be filled by 0s (zeros). When performed on a negative value in a signed type, the result is technically implementation-defined (compiler dependent), 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 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 << 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 (11100101) 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 The following program adds two operands using AND, XOR and left shift (<<).

Bitwise assignment operators C provides a compound assignment operator for each binary arithmetic and bitwise operation. Each operator accepts a left operand and a right operand, performs the appropriate binary operation on both and stores the result in the left operand. The bitwise assignment operators are as follows.

Logical equivalents 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.

!= 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 will not 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 Bit manipulation Bitwise operation Find first set Operators in C and C++ Bitboard Boolean algebra (logic) XOR swap algorithm XOR linked list

References

External links Bitwise Operators Demystifying bitwise operations, a gentle C tutorial

Worked examples

Example 1 — a first encounter with Bitwise operations in C

Start with the simplest possible case. Write down what Bitwise operations in C claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Bitwise operations in C before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Bitwise operations in C ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Bitwise operations in C

In research
Bitwise operations in C appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Bitwise operations in C in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Bitwise operations in C is common in secondary-school and first-year university syllabi. It links to neighbouring topics Binary arithmetic, C (programming language), so understanding it makes those chapters shorter.
In everyday life
Look for Bitwise operations in C outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Bitwise operations in C” →

How to study Bitwise operations in C in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Bitwise operations in C means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Bitwise operations in C out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Bitwise operations in C in simple terms?

In the C programming language, operations can be performed on a bit level using bitwise operators. Bitwise operations are contrasted by byte-level operations which characterize the bitwise operators' logical counterparts, the AND, OR, NOT operators.

Why does Bitwise operations in C matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Bitwise operations in C?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Bitwise operations in C.

Tags

  • Binary arithmetic
  • C (programming language)

Keep exploring