• C - Introduction
  • C - Comments
  • C - Data Types
  • C - Type Casting
  • C - Operators
  • C - Strings
  • C - Booleans
  • C - If Else
  • C - While Loop
  • C - For Loop
  • C - goto Statement
  • C - Continue Statement
  • C - Break Statement
  • C - Functions
  • C - Scope of Variables
  • C - Pointers
  • C - Typedef
  • C - Format Specifiers
  • C Standard Library
  • C - Data Structures
  • C - Examples
  • C - Interview Questions

AlphaCodingSkills

  • Programming Languages
  • Web Technologies
  • Database Technologies
  • Microsoft Technologies
  • Python Libraries
  • Data Structures
  • Interview Questions
  • PHP & MySQL
  • C++ Standard Library
  • Java Utility Library
  • Java Default Package
  • PHP Function Reference

C - Bitwise OR and assignment operator

The Bitwise OR and assignment operator (|=) assigns the first operand a value equal to the result of Bitwise OR operation of two operands.

(x |= y) is equivalent to (x = x | y)

The Bitwise OR operator (|) is a binary operator which takes two bit patterns of equal length and performs the logical OR operation on each pair of corresponding bits. It returns 1 if either or both bits at the same position are 1, else returns 0.

The example below describes how bitwise OR operator works:

The code of using Bitwise OR operator (|) is given below:

The output of the above code will be:

Example: Find largest power of 2 less than or equal to given number

Consider an integer 1000. In the bit-wise format, it can be written as 1111101000. However, all bits are not written here. A complete representation will be 32 bit representation as given below:

Performing N |= (N>>i) operation, where i = 1, 2, 4, 8, 16 will change all right side bit to 1. When applied on 1000, the result in 32 bit representation is given below:

Adding one to this result and then right shifting the result by one place will give largest power of 2 less than or equal to 1000.

The below code will calculate the largest power of 2 less than or equal to given number.

The above code will give the following output:

AlphaCodingSkills Android App

  • Data Structures Tutorial
  • Algorithms Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQLi Tutorial
  • Java Tutorial
  • Scala Tutorial
  • C++ Tutorial
  • C# Tutorial
  • PHP Tutorial
  • MySQL Tutorial
  • SQL Tutorial
  • PHP Function reference
  • C++ - Standard Library
  • Java.lang Package
  • Ruby Tutorial
  • Rust Tutorial
  • Swift Tutorial
  • Perl Tutorial
  • HTML Tutorial
  • CSS Tutorial
  • AJAX Tutorial
  • XML Tutorial
  • Online Compilers
  • QuickTables
  • NumPy Tutorial
  • Pandas Tutorial
  • Matplotlib Tutorial
  • SciPy Tutorial
  • Seaborn Tutorial

Learn C practically and Get Certified .

Popular Tutorials

Popular examples, reference materials.

Created with over a decade of experience.

Certification Courses

Created with over a decade of experience and thousands of feedback.

C Introduction

  • Getting Started with C
  • Your First C Program

C Fundamentals

  • C Variables, Constants and Literals
  • C Data Types
  • C Input Output (I/O)

C Programming Operators

C Flow Control

  • C if...else Statement
  • C while and do...while Loop
  • C break and continue
  • C switch Statement
  • C goto Statement
  • C Functions
  • C User-defined functions
  • Types of User-defined Functions in C Programming
  • C Recursion
  • C Storage Class

C Programming Arrays

  • C Multidimensional Arrays
  • Pass arrays to a function in C

C Programming Pointers

  • Relationship Between Arrays and Pointers
  • C Pass Addresses and Pointers
  • C Dynamic Memory Allocation
  • C Array and Pointer Examples
  • C Programming Strings
  • String Manipulations In C Programming Using Library Functions
  • String Examples in C Programming

C Structure and Union

  • C structs and Pointers
  • C Structure and Function

C Programming Files

  • C File Handling
  • C Files Examples

C Additional Topics

  • C Keywords and Identifiers

C Precedence And Associativity Of Operators

  • C Bitwise Operators
  • C Preprocessor and Macros
  • C Standard Library Functions

C Tutorials

  • Convert Binary Number to Decimal and vice-versa
  • Convert Binary Number to Octal and vice-versa
  • Make a Simple Calculator Using switch...case

Bitwise Operators in C Programming

In the arithmetic-logic unit (which is within the CPU), mathematical operations like: addition, subtraction, multiplication and division are done in bit-level. To perform bit-level operations in C programming, bitwise operators are used.

Bitwise AND Operator &

The output of bitwise AND is 1 if the corresponding bits of two operands is 1 . If either bit of an operand is 0 , the result of corresponding bit is evaluated to 0 .

In C Programming, the bitwise AND operator is denoted by & .

Let us suppose the bitwise AND operation of two integers 12 and 25 .

Example 1: Bitwise AND

Bitwise or operator |.

The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1 . In C Programming, bitwise OR operator is denoted by | .

Example 2: Bitwise OR

Bitwise xor (exclusive or) operator ^.

The result of bitwise XOR operator is 1 if the corresponding bits of two operands are opposite. It is denoted by ^ .

Example 3: Bitwise XOR

Bitwise complement operator ~.

Bitwise complement operator is a unary operator (works on only one operand). It changes 1 to 0 and 0 to 1 . It is denoted by ~ .

Twist in Bitwise Complement Operator in C Programming

The bitwise complement of 35 ( ~35 ) is -36 instead of 220 , but why?

For any integer n , bitwise complement of n will be -(n + 1) . To understand this, you should have the knowledge of 2's complement.

2's Complement

Two's complement is an operation on binary numbers. The 2's complement of a number is equal to the complement of that number plus 1. For example:

The bitwise complement of 35 is 220 (in decimal). The 2's complement of 220 is -36 . Hence, the output is -36 instead of 220 .

Bitwise Complement of Any Number N is -(N+1). Here's how:

Example 4: Bitwise complement

Shift operators in c programming.

There are two shift operators in C programming:

  • Right shift operator
  • Left shift operator.

Right Shift Operator

Right shift operator shifts all bits towards right by certain number of specified bits. It is denoted by >> .

Left Shift Operator

Left shift operator shifts all bits towards left by a certain number of specified bits. The bit positions that have been vacated by the left shift operator are filled with 0 . The symbol of the left shift operator is << .

Example #5: Shift Operators

Table of contents.

  • Bitwise AND
  • Bitwise XOR
  • Bitwise complement
  • Shift right

Sorry about that.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks .

Learn and improve your coding skills like never before.

  • Interactive Courses
  • Certificates
  • 2000+ Challenges

Related Tutorials

C Ternary Operator

  • Practice Bitwise Algorithms
  • MCQs on Bitwise Algorithms
  • Tutorial on Biwise Algorithms
  • Binary Representation
  • Bitwise Operators
  • Bit Swapping
  • Bit Manipulation
  • Count Set bits
  • Setting a Bit
  • Clear a Bit
  • Toggling a Bit
  • Left & Right Shift
  • Checking Power of 2
  • Important Tactics
  • Bit Manipulation for CP
  • Fast Exponentiation

Complete Reference for Bitwise Operators in Programming/Coding

There exists no programming language that doesn't use Bit Manipulations. Bit manipulation is all about these bitwise operations. They improve the efficiency of programs by being primitive, fast actions. There are different bitwise operations used in bit manipulation. These Bitwise Operators operate on the individual bits of the bit patterns. Bit operations are fast and can be used in optimizing time complexity.

Table of Content

1. Bitwise AND Operator (&)

2. ​bitwise or operator (|), 3. ​bitwise xor operator (^), 4. ​bitwise not operator (~), 5. left-shift (<<), 6. right-shift (>>).

Some common bit operators are:

Bitwise Operator Truth Table

The bitwise AND operator is denoted using a single ampersand symbol, i.e. &. The & operator takes two equal-length bit patterns as parameters. The two-bit integers are compared. If the bits in the compared positions of the bit patterns are 1, then the resulting bit is 1. If not, it is 0.

Truth table of AND operator

Take two bit values X and Y, where X = 7= (111) 2 and Y = 4 = (100) 2 . Take Bitwise and of both X & y Bitwise ANDof (7 & 4)

Implementation of AND operator:

Time Complexity: O(1)  Auxiliary Space: O(1)

The | Operator takes two equivalent length bit designs as boundaries; if the two bits in the looked-at position are 0, the next bit is zero. If not, it is 1.

BItwiseORoperatortruthtable-300x216-(2)

Take two bit values X and Y, where X = 7= (111) 2 and Y = 4 = (100) 2 . Take Bitwise OR of both X, y Bitwise OR of (7 | 4) Explanation: On the basis of truth table of bitwise OR operator we can conclude that the result of  1 | 1  = 1 1 | 0 = 1 0 | 1 = 1 0 | 0 = 0 We used the similar concept of bitwise operator that are show in the image.

Implementation of OR operator:

The ^ operator (also known as the XOR operator) stands for Exclusive Or. Here, if bits in the compared position do not match their resulting bit is 1. i.e, The result of the bitwise XOR operator is 1 if the corresponding bits of two operands are opposite, otherwise 0.

BItwiseXORoperatortruthtable-300x216-(1)

Take two bit values X and Y, where X = 7= (111) 2 and Y = 4 = (100) 2 . Take Bitwise and of both X & y Bitwise OR of (7 ^ 4) Explanation: On the basis of truth table of bitwise XOR operator we can conclude that the result of  1 ^ 1  = 0 1 ^ 0 = 1 0 ^ 1 = 1 0 ^ 0 = 0 We used the similar concept of bitwise operator that are show in the image.

Implementation of XOR operator:

All the above three bitwise operators are binary operators (i.e, requiring two operands in order to operate). Unlike other bitwise operators, this one requires only one operand to operate.

The bitwise Not Operator takes a single value and returns its one’s complement. The one’s complement of a binary number is obtained by toggling all bits in it, i.e, transforming the 0 bit to 1 and the 1 bit to 0.

Truth Table of Bitwise Operator NOT

Take two bit values X and Y, where X = 5= (101) 2 . Take Bitwise NOT of X. Explanation: On the basis of truth table of bitwise NOT operator we can conclude that the result of  ~1  = 0 ~0 = 1 We used the similar concept of bitwise operator that are show in the image.

Implementation of NOT operator:

The left shift operator is denoted by the double left arrow key ( << ). The general syntax for left shift is shift-expression << k. The left-shift operator causes the bits in shift expression to be shifted to the left by the number of positions specified by k . The bit positions that the shift operation has vacated are zero-filled. 

Note: Every time we shift a number towards the left by 1 bit it multiply that number by 2.

Logical left Shift

Input: Left shift of 5 by 1. Binary representation of 5 = 00101 and Left shift of 00101 2 by 1 (i.e, 00101 << 1)   Left shift of 5 by 1 Output: 10 Explanation: All bit of 5 will be shifted by 1 to left side and this result in 01010 2, Which is equivalent to 10 Input: Left shift of 5 by 2. Binary representation of 5 = 00101 and Left shift of 00101 2 by 1 (i.e, 00101 << 2) Left shift of 5 by 2 Output: 20 Explanation: All bit of 5 will be shifted by 1 to left side and this result in 10100 2, Which is equivalent to 20 Input: Left shift of 5 by 3. Binary representation of 5 = 00101 and Left shift of 00101 2 by 1 (i.e, 00101 << 3) Left shift of 5 by 3 Output: 40 Explanation: All bit of 5 will be shifted by 1 to left side and this result in 01000 2, Which is equivalent to 40

Implementation of Left shift operator:

The right shift operator is denoted by the double right arrow key (>>) . The general syntax for the right shift is " shift-expression >> k". The right-shift operator causes the bits in shift expression to be shifted to the right by the number of positions specified by k . For unsigned numbers, the bit positions that the shift operation has vacated are zero-filled. For signed numbers, the sign bit is used to fill the vacated bit positions. In other words, if the number is positive, 0 is used, and if the number is negative, 1 is used.

Note: Every time we shift a number towards the right by 1 bit it divides that number by 2.

Logical Right Shift

Input: Left shift of 5 by 1. Binary representation of 5 = 00101 and Left shift of 00101 2 by 1 (i.e, 00101 << 1) Right shift of 5 by 1 Output: 10 Explanation: All bit of 5 will be shifted by 1 to left side and this result in 01010 2, Which is equivalent to 10 Input: Left shift of 5 by 2. Binary representation of 5 = 00101 and Left shift of 00101 2 by 1 (i.e, 00101 << 2) Right shift of 5 by 2 Output: 20 Explanation: All bit of 5 will be shifted by 1 to left side and this result in 10100 2, Which is equivalent to 20 Input: Left shift of 5 by 3. Binary representation of 5 = 00101 and Left shift of 00101 2 by 1 (i.e, 00101 << 3) Right shift of 5 by 3 Output: 40 Explanation: All bit of 5 will be shifted by 1 to left side and this result in 01000 2, Which is equivalent to 40

Implementation of Right shift operator:

Application of BIT Operators

  • Bit operations are used for the optimization of embedded systems.
  • The Exclusive-or operator can be used to confirm the integrity of a file, making sure it has not been corrupted, especially after it has been in transit.
  • Bitwise operations are used in Data encryption and compression.
  • Bits are used in the area of networking, framing the packets of numerous bits which are sent to another system generally through any type of serial interface.
  • Digital Image Processors use bitwise operations to enhance image pixels and to extract different sections of a microscopic image.

author

Similar Reads

  • Complete Reference for Bitwise Operators in Programming/Coding There exists no programming language that doesn't use Bit Manipulations. Bit manipulation is all about these bitwise operations. They improve the efficiency of programs by being primitive, fast actions. There are different bitwise operations used in bit manipulation. These Bitwise Operators operate 13 min read
  • Bitwise Left Shift(<<) Operator in Programming Bitwise operators play a significant role in manipulating individual bits within binary data. Among these operators, the Bitwise Left Shift (<<) operator is used for shifting the bits of a number to the left by a specified number of positions. In this blog post, we'll explore the definition, s 5 min read
  • Bitwise XOR Operator in Programming Bitwise XOR Operator is represented by the caret symbol (^). It is used to perform a bitwise XOR operation on the individual bits of two operands. The XOR operator returns 1 if the corresponding bits in the two operands are different, and 0 if they are the same. Table of Content What is Bitwise XOR? 5 min read
  • Bitwise OR Operator (|) in Programming In programming, Bitwise Operators play a crucial role in manipulating individual bits of data. One of the fundamental bitwise operators is the Bitwise OR operator (|). In this article, we’ll discuss the Bitwise OR operator, its syntax, properties, applications, and optimization techniques, and concl 5 min read
  • Bitwise Hacks for Competitive Programming Prerequisite: It is recommended to refer Interesting facts about Bitwise Operators How to set a bit in the number 'num': If we want to set a bit at nth position in the number 'num', it can be done using the 'OR' operator( | ). First, we left shift '1' to n position via (1<<n)Then, use the 'OR' 14 min read
  • Bitwise AND operator in Programming In programming, Bitwise Operators play a crucial role in manipulating individual bits of data. One of the fundamental bitwise operators is the Bitwise AND operator (&). In this article, we'll dive deep into what is Bitwise AND operator, its syntax, properties, applications, and optimization tech 6 min read
  • Assignment Operators in Programming Assignment operators in programming are symbols used to assign values to variables. They offer shorthand notations for performing arithmetic operations and updating variable values in a single step. These operators are fundamental in most programming languages and help streamline code while improvin 7 min read
  • Program for Bitwise Operators in C, C++, Java, Python, C# & JavaScript Bitwise operators are fundamental operators used in computer programming for manipulating individual data bits. They operate at the binary level, performing operations on binary data representations. These operators are commonly used in low-level programming, such as device drivers, embedded systems 9 min read
  • Bit Manipulation for Competitive Programming Bit manipulation is a technique in competitive programming that involves the manipulation of individual bits in binary representations of numbers. It is a valuable technique in competitive programming because it allows you to solve problems efficiently, often reducing time complexity and memory usag 15+ min read
  • Binary Operators in Programming Binary Operators are essential tools in programming that perform operations on pairs of data, enabling tasks like calculations, comparisons, logical operations, and bitwise manipulations. They are fundamental for processing and manipulating data efficiently in computer programs. Table of Content Wha 14 min read
  • Arithmetic Operators in Programming Arithmetic operators are fundamental components of programming languages that allow developers to perform mathematical operations on numerical data types. These operators enable manipulation of numeric values, making them essential for various computational tasks. Table of Content What are Arithmeti 7 min read
  • Conditional Operator in Programming Conditional Operator, often referred to as the ternary operator, is a concise way to express a conditional (if-else) statement in many programming languages. It is represented by the "?" symbol and is sometimes called the ternary operator because it takes three operands. Table of Content Syntax of C 9 min read
  • Types of Operators in Programming Types of operators in programming are symbols or keywords that represent computations or actions performed on operands. Operands can be variables, constants, or values, and the combination of operators and operands form expressions. Operators play a crucial role in performing various tasks, such as 15+ min read
  • Bit Tricks for Competitive Programming In competitive programming or in general, some problems seem difficult but can be solved very easily with little concepts of bit magic. We have discussed some tricks below in the previous post.Bitwise Hacks for Competitive Programming One-Liner Hacks of Bit Manipulation:One-Liner CodeFunctionx&1 7 min read
  • Logical NOT Operator in Programming In programming, Logical operators play a crucial role in manipulating data. One of the fundamental logical operators is the Logical NOT operator(!).In this article, we’ll discuss the Logical NOT operator, its syntax, properties, applications, and optimization techniques, and conclude with its signif 5 min read
  • Logical Operators in Programming Logical Operators are essential components of programming languages that allow developers to perform logical operations on boolean values. These operators enable developers to make decisions, control program flow, and evaluate conditions based on the truthiness or falsiness of expressions. In this a 4 min read
  • C++ tricks for competitive programming (for C++ 11) We have discussed some tricks in the below post. In this post, some more tricks are discussed. Writing C/C++ code efficiently in Competitive programming Although, practice is the only way that ensures increased performance in programming contests but having some tricks up your sleeve ensures an uppe 5 min read
  • Bitwise Complement Operator (~ tilde) Pre-requisite:Bitwise Operators in C/ C++Bitwise Operators in Java The bitwise complement operator is a unary operator (works on only one operand). It takes one number and inverts all bits of it. When bitwise operator is applied on bits then, all the 1's become 0's and vice versa. The operator for t 3 min read
  • Operator Associativity in Programming Operator associative refers to the order in which operators of the same precedence are used in a word. In a programming language, it is important to understand the interactions between operators to properly define and test expressions. In this article, we will discuss operator associativity in progr 14 min read
  • Programming
  • Bitwise-AND
  • Bitwise-XOR

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

CProgramming Tutorial

  • C Programming Tutorial
  • Basics of C
  • C - Overview
  • C - Features
  • C - History
  • C - Environment Setup
  • C - Program Structure
  • C - Hello World
  • C - Compilation Process
  • C - Comments
  • C - Keywords
  • C - Identifiers
  • C - User Input
  • C - Basic Syntax
  • C - Data Types
  • C - Variables
  • C - Integer Promotions
  • C - Type Conversion
  • C - Type Casting
  • C - Booleans
  • Constants and Literals in C
  • C - Constants
  • C - Literals
  • C - Escape sequences
  • C - Format Specifiers
  • Operators in C
  • C - Operators
  • C - Arithmetic Operators
  • C - Relational Operators
  • C - Logical Operators
  • C - Bitwise Operators
  • C - Assignment Operators
  • C - Unary Operators
  • C - Increment and Decrement Operators
  • C - Ternary Operator
  • C - sizeof Operator
  • C - Operator Precedence
  • C - Misc Operators
  • Decision Making in C
  • C - Decision Making
  • C - if statement
  • C - if...else statement
  • C - nested if statements
  • C - switch statement
  • C - nested switch statements
  • C - While loop
  • C - For loop
  • C - Do...while loop
  • C - Nested loop
  • C - Infinite loop
  • C - Break Statement
  • C - Continue Statement
  • C - goto Statement
  • Functions in C
  • C - Functions
  • C - Main Function
  • C - Function call by Value
  • C - Function call by reference
  • C - Nested Functions
  • C - Variadic Functions
  • C - User-Defined Functions
  • C - Callback Function
  • C - Return Statement
  • C - Recursion
  • Scope Rules in C
  • C - Scope Rules
  • C - Static Variables
  • C - Global Variables
  • Arrays in C
  • C - Properties of Array
  • C - Multi-Dimensional Arrays
  • C - Passing Arrays to Function
  • C - Return Array from Function
  • C - Variable Length Arrays
  • Pointers in C
  • C - Pointers
  • C - Pointers and Arrays
  • C - Applications of Pointers
  • C - Pointer Arithmetics
  • C - Array of Pointers
  • C - Pointer to Pointer
  • C - Passing Pointers to Functions
  • C - Return Pointer from Functions
  • C - Function Pointers
  • C - Pointer to an Array
  • C - Pointers to Structures
  • C - Chain of Pointers
  • C - Pointer vs Array
  • C - Character Pointers and Functions
  • C - NULL Pointer
  • C - void Pointer
  • C - Dangling Pointers
  • C - Dereference Pointer
  • C - Near, Far and Huge Pointers
  • C - Initialization of Pointer Arrays
  • C - Pointers vs. Multi-dimensional Arrays
  • Strings in C
  • C - Strings
  • C - Array of Strings
  • C - Special Characters
  • C Structures and Unions
  • C - Structures
  • C - Structures and Functions
  • C - Arrays of Structures
  • C - Self-Referential Structures
  • C - Lookup Tables
  • C - Dot (.) Operator
  • C - Enumeration (or enum)
  • C - Structure Padding and Packing
  • C - Nested Structures
  • C - Anonymous Structure and Union
  • C - Bit Fields
  • C - Typedef
  • File Handling in C
  • C - Input & Output
  • C - File I/O (File Handling)
  • C Preprocessors
  • C - Preprocessors
  • C - Pragmas
  • C - Preprocessor Operators
  • C - Header Files
  • Memory Management in C
  • C - Memory Management
  • C - Memory Address
  • C - Storage Classes
  • Miscellaneous Topics
  • C - Error Handling
  • C - Variable Arguments
  • C - Command Execution
  • C - Math Functions
  • C - Static Keyword
  • C - Random Number Generation
  • C - Command Line Arguments
  • C Programming Resources
  • C - Questions & Answers
  • C - Quick Guide
  • C - Cheat Sheet
  • C - Useful Resources
  • C - Discussion
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary

Assignment Operators in C

In C language, the assignment operator stores a certain value in an already declared variable. A variable in C can be assigned the value in the form of a literal, another variable, or an expression.

The value to be assigned forms the right-hand operand, whereas the variable to be assigned should be the operand to the left of the " = " symbol, which is defined as a simple assignment operator in C.

In addition, C has several augmented assignment operators.

The following table lists the assignment operators supported by the C language −

Simple Assignment Operator (=)

The = operator is one of the most frequently used operators in C. As per the ANSI C standard, all the variables must be declared in the beginning. Variable declaration after the first processing statement is not allowed.

You can declare a variable to be assigned a value later in the code, or you can initialize it at the time of declaration.

You can use a literal, another variable, or an expression in the assignment statement.

Once a variable of a certain type is declared, it cannot be assigned a value of any other type. In such a case the C compiler reports a type mismatch error.

In C, the expressions that refer to a memory location are called "lvalue" expressions. A lvalue may appear as either the left-hand or right-hand side of an assignment.

On the other hand, the term rvalue refers to a data value that is stored at some address in memory. A rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right-hand side but not on the left-hand side of an assignment.

Variables are lvalues and so they may appear on the left-hand side of an assignment. Numeric literals are rvalues and so they may not be assigned and cannot appear on the left-hand side. Take a look at the following valid and invalid statements −

Augmented Assignment Operators

In addition to the = operator, C allows you to combine arithmetic and bitwise operators with the = symbol to form augmented or compound assignment operator. The augmented operators offer a convenient shortcut for combining arithmetic or bitwise operation with assignment.

For example, the expression "a += b" has the same effect of performing "a + b" first and then assigning the result back to the variable "a".

Run the code and check its output −

Similarly, the expression "a <<= b" has the same effect of performing "a << b" first and then assigning the result back to the variable "a".

Here is a C program that demonstrates the use of assignment operators in C −

When you compile and execute the above program, it will produce the following result −

avatar

Assignment Operators In C | A Complete Guide With Detailed Examples

Assignment Operators In C | A Complete Guide With Detailed Examples

Table of content: 

What are assignment operators in c, how assignment operators in c work step-by-step explanation, types of assignment operators in c, bitwise assignment operators in c.

  • Precedence & Associativity Of Assignment Operators In C

Table Of Assignment Operators In C

Relational vs. assignment operators in c, frequently asked questions.

Operators are symbols or special characters that perform specific operations on one or more operands. They enable various computations and manipulation of data/ operands in C programs. These operands can be variables, constants, or expressions. It is important to understand the concept of various types of operators to utilize them effectively in writing efficient C code. In this article, we will focus on assignment operators in C.

Other types of operators include arithmetic, bitwise, logical, relational, ternary/ conditional, etc. Collectively, they enable complex computations, comparisons, and control flow within programs, allowing developers to manipulate data in a variety of ways.

In simple terms, assignment operators in C programming are used to assign values to variables. This includes assigning initial value as well as updating the value of a variable with a new value based on a certain operation or calculation. The most commonly used assignment operator is the equal-to operator (=), which assigns the right-hand side value to the variable on the left-hand side of the operator.

For example, the expression x = 5 assigns the value of 5 to the variable x.

Other assignment operators in C include addition assignment (+=), subtraction assignment (-=), multiplication assignment (*=), division assignment (/=), and modulus/ modulo assignment (%=). They are also referred to as compound assignment operators. As their name suggests, they are used to assign the resultant value of addition, subtraction, multiplication, division, and modulus operations (respectively) to the left-side variable/ operand.

For example, the equation x += 5 is equivalent to x = x + 5. It updates the value of x by adding 5 to its current value.

These assignment operators can be combined with other operators to perform more complex operations in a single statement. For example, x += y * 3 updates the value of x by adding three times the value of y to its current value.

Using basic assignment operators can help make code more concise and easier to read and improve performance by reducing the number of operations required. However, it is important to use them carefully and avoid unintended consequences, such as side effects or errors. We will discuss each of these assignment operators in detail ahead, but first let's look at the basic syntax for this.

Syntax For Assignment Operators In C:

variable_name operator expression;

Here, 

  • The variable_name refers to the name/ identifier of the variable to whom you are assigning (or updating) a value. 
  • The term operator refers to the respective assignment operator you are using.
  • Expression  is the right-hand operand, i.e., it refers to the value you assign to the variable. 

Here are some examples of assignment operators and their syntax:

x = 5; // assigns the value of 5 to the variable x y += 10; // adds 10 to the current value of y and updates y with the result z *= 3; // multiplies the current value of z by 3 and updates z with the result

Given below is a stepwise description of the working mechanism of assignment operators in C.

  • Step 1 (The variable): The first step is to select the variable that we want to assign a value to. This variable can be of any data type, such as integer, float, or char .
  • Step 2 (The operator):  The second step is to select the assignment operator that we want to use. As mentioned, the most commonly used assignment operator is the simple assignment operator (=), to assign the value of the right side operand to the left side operand.
  • Step 3 (The value or expression): The third step is to provide the value (or expression for calculating value) we want to assign to the variable. This value can be a constant or an expression that combines variables and constants using arithmetic or logical operators .
  • Step 4 (The evaluation): After that, the compiler evaluates the expression on the right-hand side of the operator. This evaluation involves performing any necessary logical or arithmetic operations based on the precedence rules of the operators.
  • Step 5 (The assignment): Finally, it assigns the evaluated value to the variable on the left-hand side of the assignment operator. If we are updating the value, this assignment replaces the previous value of the variable with the new value.

Let's look at a basic C program example illustrating both these approaches of using assignment operators.

Code Example:

The initial values of x and y are: 5, 10 The updated value of x is: 15

Explanation:

We begin the simple C program example by including the <stdio.h> header file for input/output operations. 

  • We then initiate the main() function , which is the entry point of the program's execution. 
  • Inside main(), we declare two integer variables , x and y, and assign the values of 5 to x and 10 to y. Then, we print these initial values to the console using the printf() function .
  • Next, we use the addition assignment operator (as mentioned in the code comments ) to add the initial value of y to the initial value of x and assign the result to x.
  • The operation involves adding the value of y, which is 5, to the value of x, which is 10. The output is, hence, 15.
  • Once again, we use the printf() function to display the updated value of x.

All the assignment operators in C programming language can be classified into two major types with subtypes. These types and the operators included in them are mentioned below-

Simple Assignment Operator In C (=)

There is only one assignment operator that falls into this category. This is the simplest assignment operator whose only purpose is to assign the value given on the right to the variable on the left. The basic assignment operator symbol is a single equals sign (=). It is the most commonly used assignment operator in C language. Below is an example of how to use this operator in simple assignment expressions.

The value of x is 5

In the simple C code example , we include the standard input-output header file, <stdio.h>, which contains the library functions for input and output operations in C programming.

  • Inside main() funtion , we declare an integer variable named x and initialize it with the value 5 using the simple assignment operator .
  • This step reserves a memory location to store an integer value and assigns the value 5 to x.
  • Next, we use the printf() function to display the output with a descriptive string message (using formatted string) .
  • Here, we use the  %d format specifier  as a placeholder for the integer value and the newline escape sequence (\n) to shift the cursor to the next line afterwards. 
  • Lastly, the program successfully completes execution with the return 0 statement . It indicates the successful termination of the main() function and the program as a whole.

It will be easier to understand why we call this operator simple when you check out the other category, i.e., compound operators. 

Compound Assignment Operators In C

The compound assignment operators are called as such because they combine a binary operation with the assignment operation. They perform a binary operation on the values of the variable and the value on the right-hand side and then assign the result to the variable on the left-hand side/ left operand.

There are five types of compound assignment operator in C:

  • Subtraction Assignment Operator (-=)
  • Addition Assignment Operator (+=)
  • Multiplication Assignment Operator (=)
  • Division Assignment Operator (/=)
  • Modulus Assignment Operator (%=)

The advantage of using these operators is that they provide a shorthand notation for performing common operations such as addition, multiplication, division, etc. 

Now that you have a broad idea of what compound assignment operators in C are, let's explore each type of assignment operator in detail. 

Addition Assignment Operator In C (+=)

The addition assignment operator adds the value given on its right side to the current value of the variable on the left. It then assigns the resultant value back to the variable on the left.

In other words, it adds the initial value of the first variable to the initial value of a second variable and assigns it back to the first variable. Below is a C program example illustrating the use of this compound assignment operator. 

The value of x is 8

In the  C code example, we first include the essential header files.

  • Inside the main() function , we declare an integer variable, x , and initialize it with the value 5 using the simple assignment operator. 
  • Next, we use the addition assignment operator (+=) to add a value 3 to x (variable on the left side) and assign the result back to it.
  • Here, we add 3 to x's initial value and reassign it back. That is,  x += 3 is equivalent to x = x + 3, which adds 3 to x's value and stores the result back into x.
  • Then, we use the printf() function to display the output to the console, after which the program terminates with the return 0 statement.
Check out this amazing course to become the best version of the C programmer you can be.

Subtraction Assignment Operator In C (-=)

The subtraction assignment operator is used to subtract a given value, mentioned on the right side of the operator, from the initial value of the variable on the left (i.e., the left operand). It then assigns the result back to the original variable on the left. Alternatively, we can also subtract the value of a second variable from the initial variable.

The value of x is 7

In the example C program - 

  • Inside the main() function , we declare an integer variable x and assign the value 10 to it using the simple operator.
  • We then use the subtraction assignment operator (-=) to subtract a value 3 from the initial value of x. The result is assigned back to x using this operator. 
  • Here, the expression x -= 3 is equivalent to x = x - 3, which means we subtract 3 from 5 (i.e., the value of x), and the value of x is updated with the result.
  • After that, we use the printf() function to display the updated value of the variable x to the console.

Multiplication Assignment Operator In C (*=)

The multiplication assignment operator in C (*=) first multiplies a given value (on the right) with the initial/ current value of the variable on the left (left operand). Then, it assigns the result back to the variable on the left. Alternatively, it can also be used to multiply the initial value of a variable by the value of another variable in the program. 

The value of x is 20

In the example C code -

  • Inside the main() function , we declare an integer variable named x and initialize it with the value 5 using the simple assignment operator.
  • Next, we use the multiplication assignment operator to multiply the initial value of x by 4 and then assign the result back to variable x.
  • That is, the expression x *= 4 implies x = x * 4, which multiplies the value of x by 4 and stores the result back into x.
  • Using the printf() function , we display the updated value of x and a string literal message.

Division Assignment Operator In C (/=)

The division assignment operator (/=) first divides the current/ initial value of the variable mentioned on its left side by the value given on its right. Then, the revised value/ result is assigned back to the variable on the left. Alternatively, we can also use this operator to divide the value of one variable by another variable and then update the value of one. 

Let's take a look at an example that illustrates the use of this compound assignment operator in C code.

In the sample C program -

  • We declare an integer variable  x inside the main() function and initialize it with the value 10. 
  • Next, we use the  division assignment operator (/=)  to divide the value of x by 2 and assign the result of this operation back to variable x.  
  • Here, the expression x /= 2 can be expanded as x = x / 2, which ultimately divides the value of x by 2 and stores the result back into x.
  • Finally, we display this output using the printf() function with a formatted string message.

Modulo Assignment Operator In C (%=)

The modulo assignment operator (%=) is a combination of the modulo arithmetic operator and assignment operator. It divides the value of the left-side operand by the right-side operand to calculate the remainder of this division. Then, it assigns the resulting remainder back to the variable on the left. Note that the value on the right can also be the value of another variable in the program. 

Look at the example below to better understand how to apply this compound assignment operator in C programs.

The value of x is 3

In the sample C code -

  • In the main() function , we declare a variable, x, of integer data type and initialize it with the value of 14. 
  • Then, we use the modulo assignment operator ( %=) to calculate the remainder from the division of the variable on the left side by a specified value and assign the result back to that variable.
  • In this example, we divide the value of x by 4 and reassign the remainder to it.
  • That is, x %= 4 is equivalent to x = x % 4, which ultimately calculates the remainder when x is divided by 4 and stores the result back into x.
  • This reassigned/ updated value of x is displayed on the console using the printf() function with a formatted string and %d format specifier. 

We have already discussed the most commonly used assignment operators in C programing language. There are a few other compound assignment operators that you must know about. These are the bitwise assignment operators, which first conduct the respective bitwise operation on the operands and then assign the result back to the left operand in the expression.

There are three types of bitwise assignment operators in C, which we have discussed ahead. 

Bitwise AND Assignment Operator In C (&=)

The bitwise AND assignment operator (&=) performs a bitwise AND operation between the current value of the variable on the left (i.e., the left operand) and the value on the right (i.e., the right operand) and then assigns the result to the variable on the left. The value on the right can also be another variable.

The assignment operator example below showcases how this compound operator works.

The value of x is 4

In the C program sample , we define the main() function (which serves as the entry point) after including the <stdio.h> file.

  • We then declare an integer variable called  x and use the basic assignment operator to initialize it with the value 12. 
  • Next, we use the  bitwise AND assignment operator (&=) to perform a bitwise AND operation between the value of the variable on the left side and a specified value, and then we assign the result back to that variable.
  • The equation  x &= 5 , which implies x = x & 5, performs a bitwise AND between the value of x and 5 and stores the result back into x.
  • We display the result of this operation to the console with the printf() function , a formatted string, and the %d specifier.
Hone your coding skills with the 100-Day Coding Sprint at Unstop and claim bragging rights now!

Bitwise OR Assignment Operator In C (|=)

The bitwise OR assignment operator (|=) performs a bitwise OR operation between the current value of the variable on the left and the value on the right and then assigns the result to the variable on the left. The example below showcases the use this compound assingment operator in C.

Output Window :

The value of x is 13

Code Explanation:

In the C code sample -

  • In the main() function , we declare an integer variable x  and initialize it with the value 12. This reserves a memory location to store an integer value.
  • Next, using the bitwise OR assignment operator , we perform a bitwise OR operation between the value of the variable on the left side (i.e., 12) and a specified value (i.e., 5) and assign the result back to the left-side variable, i.e., x.
  • The expression x |= 5 leads to the evaluation of x = x | 5, which performs a bitwise OR between the value of x and 5 and stores the result back into x.
  • Then, we use the printf() function to display the result/ updated value of left operand x to the console. We also use a formatted string and the %d specifier inside printf().

Left Shift Assignment Operator In C (<<=)

The bitwise left shift assignment operator (<<=) first shifts the bits of the current value of the variable on the left (i.e., left-hand operand) by the number of positions specified by the value on the right. Then, it assigns the result of this operation back to the variable on the left.

The value of x is 40

In this assignment operator example, we first include the essential header file and then initiate the main() function .

  • Inside main(), we declare an integer variable x and initialize it with the value 10. This step reserves a memory location to store an integer value and assigns the value 10 to x.
  • Next, we apply the left shift assignment operator (<<=) on this variable to perform a left shift operation on its value by a specified number of bits (here, 2) and assign the result back to that variable.
  • The equation x <<= 2 expands to x = x << 2, which performs a left shift on the value of x by 2 bits and stores the result back into x.
  • We print the new value of the left-hand side variable x to the console using the printf() function .  

Right Shift Assignment Operator In C (>>=)

The bitwise right shift assignment operator (>>=) begins by shifting the bits of the initial value of the variable on the left (i.e., left-hand operand) by the number of positions specified by the right-hand value and then assigns the result to the variable on the left.

In the assignment operator C example-

  • Inside the main() function (which serves as the entry point for program execution), we declare an integer variable x and initialize it with the value 10. 
  • Next, we employ the right shift assignment operator ( >>=) to shift the bits of the value on the left side (i.e., 10, the value of x) by a specified number of positions (i.e., 1) to the right. Then, assign the result back to the left-hand side variable.
  • The equation x >>= 1 is equivalent to x = x >> 1, which performs a right shift operation on the value of x by 1 position and stores the result back into x.
  • We publish this updated value of the variable x to the console using a formatted string and %d specifier inside the printf() function . 

Also Read: Shift Operators In C | The Ultimate Guide With Code Examples

Precedence & Associativity Of Assignment Operators In C

In C, operators have a defined precedence that determines the order in which expressions are evaluated when multiple operators are present. Precedence helps in resolving which operator should be applied or evaluated first in an expression. Associativity, on the other hand, defines the direction in which operators of the same precedence level are processed.

Precedence of Assignment Operators In C

All assignment operators in C (=, +=, -=, *=, /=, %=, etc.) have lower precedence compared to most other operators, such as arithmetic operators (+, -, *, /), relational operators (<, >, <=, >=) , and logical operators (&&, ||). This means that in an expression where assignment operators are used alongside other operators, the other operators are evaluated first before the assignment takes place.

In other words, the other operators take precedence over assignment operators when evaluating expressions in C programs. For example:

int a = 5; int b = 10; int c = a + b * 2;

In this expression, the multiplication operator (*) has higher precedence than the addition operator (+), and both have higher precedence than the assignment (=). Therefore, b * 2 is evaluated first, then the result is added to a, and finally, the sum is assigned to c.

Assignment Operator Associativity In C

Assignment operators in C have right-to-left associativity. This means that when multiple assignment operators are used in a single expression, the expression is evaluated from right to left.

int a, b, c; a = b = c = 10;

In this case, the value 10 is first assigned to c, then the value of c is assigned to b, and finally, the value of b is assigned to a. Since every assignment operator associates from right to left, this evaluation happens from right to left.

This associativity of assignment operators in C is particularly important to understand when chaining assignments or combining them with other operations.

Here is a table listing all the assignment operators in C programming with a brief description and example.

Both the relational and the assignment operators serve different purposes and have distinct characteristics in programming languages like C. Assignment operators deal with assigning values to operands/ variables. In contrast, relational operators focus on making comparisons between operands, thus enabling logical decision-making in control flow statements .

Here are some points highlighting the differences between relational and assignment operators in C:

Purpose | Relational Vs. Assignment Operators In C

Assignment Operators: Assignment operators are used to assign values or expressions to variables. They perform the task of storing a value in a variable.

Relational Operators: Relational operators are used to compare values or expressions. They determine the relationship between two operands and produce a Boolean result (true or false) based on the comparison.

Syntax | Relational Vs. Assignment Operators In C

Assignment Operators: Assignment operators are typically represented by a single character followed by an equal sign, such as (=), (+=), (-=), (*=), etc.

Relational Operators: Relational operators are represented by two or more characters, including equality operators (==, !=), other relational operators (<, >, <=, >=), etc.

Operation | Relational Vs. Assignment Operators In C

Assignment Operators: Assignment operators in C perform the task of assigning a value or expression to a variable. They modify the value of the left-hand side variable based on the right-hand side value or expression.

Relational Operators: Relational operators compare the values of two operands and return a Boolean result indicating whether the comparison is true or false.

Result Type | Relational Vs. Assignment Operators In C

Assignment Operators: In case of the simple assignment operator, the result is not a value in itself but the assignment of value to the variable on the left-hand side of the operator. But in case of compound assingment operators in C the result is the value assigned to the variable on the left-hand side of the operator, which can be used in further expressions or assignments.

Relational Operators: The result of a relational operator is a Boolean value, either true or false, indicating the outcome of the comparison.

Usage | Relational Vs. Assignment Operators In C

Assignment Operators: Assignment operators in C are primarily used for variable assignments, updating variables, and performing calculations involving the current value of a variable.

Relational Operators: Relational operators are used in conditional statements (if, while, for) and expressions that require comparison between variables or values.

Precedence | Relational Vs. Assignment Operators In C

Assignment Operators: Assignment operators in C have lower precedence compared to most other operators, which means they are evaluated after most other operations in an expression.

Relational Operators : Relational operators have higher precedence than assignment operators, allowing them to be evaluated before assignment operations.

Looking for mentors? Find the perfect mentor for select experienced coding & software experts here .

The assignment operator in C is one of the most important and commonly used operators in the language. It assigns values to variables, which is essential for writing any program. Compound assignment operators can be used to make code more concise and efficient. These assignment operators are fundamental operators that allow programmers to store and manipulate data effectively throughout their programs. It is important to understand the different types of assignment operators and how to use them correctly to write and run efficient and effective C code .

Also Read: 100+ Top C Interview Questions With Answers (2024)

Q. What is an assignment operator in C?

An assignment operator is an operator in the C language that assigns a value to a variable. This could be a direct value (as in initialization) or the value that results from a compound operation (like the result of addition, subtraction, multiplication, etc. operations).

The most commonly used assignment operator is the equal sign (=), the basic assignment operator. It assigns the value on the right-hand side to the variable on the left-hand side of the operator. 

Q. What is the difference between a simple assignment operator and a compound assignment operator?

The simple assignment operator, the equal-to operator (=), assigns a single value to a variable. On the other hand, a compound assignment operator in C (binary operator) performs a binary operation (such as addition, subtraction, multiplication, etc.) between the current value of a variable and another value or expression and then assigns the result to the variable. Its examples are- addition assignment (+=), multiplication assignment (*=), modulo assignment (%=), etc. 

Q. What happens if I use an assignment operator with incompatible types in C?

Using an assignment operator with incompatible types in the C programming language results in a compilation error . C is a statically typed language, meaning variables must be explicitly declared with their data types, and the compiler enforces strict type-checking during the compilation process . If an attempt is made to assign a value of one type to a variable of a different, incompatible type without an explicit type conversion , the compiler will raise an error.

For example, trying to assign a character ('A') to an integer variable declared as int will lead to a compilation error. This mechanism helps catch potential type-related errors early in the development process, ensuring that the program adheres to the specified type rules and preventing unexpected behavior during runtime. 

Q. Can I chain multiple assignment operators together in C language?

In the C programming language, it is possible to chain multiple assignment operators together in a single statement. That is, you can assign the same value to multiple variables in one line by chaining assignments. For example, the following code is valid in C:

int a, b, c; a = b = c = 5; // This is not allowed in C

This assigns the value 5 to all three variables (a, b, and c). The expression is evaluated from right to left due to the right-associative nature of the assignment operators in C.

Q. Are there any precedence rules for assignment operators?

Yes, there are precedence rules for assignment operators in C language. The assignment operator (=) has a lower precedence than most other operators. This means that when an expression contains both assignment and other operators, the assignment will be performed after the evaluation of the other operators.

For example:

int a, b, c; a = b = c = 5 + 3 * 2;

In this example, the multiplication (*) has higher precedence than the assignment (=). So, 3 * 2 is evaluated first, resulting in 6. Then, 5 + 6 is evaluated, resulting in 11. Finally, the assignment is performed from right to left, so c, b, and a will all be assigned the value 11.

It's important to note that associativity also plays a role. The assignment operators in C are right-associative, which means that when multiple assignments appear in a row, they are evaluated from right to left. In the example above, c = 5 + 3 * 2 is evaluated first, followed by b = c, and finally a = b.

Now that you know all about assignment operators in C, check out some other interesting topics:

  • Increment And Decrement Operators In C With Precedence (+Examples)
  • Keywords In C Language | Properties, Usage, Examples & Detailed Explanation
  • Pointers In C | Ultimate Guide With Easy Explanations (+Examples)
  • Ternary (Conditional) Operator In C Explained With Code Examples
  • For Loop In C | Structure, Working & Variations With Code Examples

An economics graduate with a passion for storytelling, I thrive on crafting content that blends creativity with technical insight. At Unstop, I create in-depth, SEO-driven content that simplifies complex tech topics and covers a wide array of subjects, all designed to inform, engage, and inspire our readers. My goal is to empower others to truly #BeUnstoppable through content that resonates. When I’m not writing, you’ll find me immersed in art, food, or lost in a good book—constantly drawing inspiration from the world around me.

No comments Image

Login to continue reading

And access exclusive content, personalized recommendations, and career-boosting opportunities.

NationBuilding Case Study Competition 2025

to our newsletter

Blogs you need to hog!

Porter's 5 Forces: Comprehensive Guide To This Analytical Tool

Porter's 5 Forces: Comprehensive Guide To This Analytical Tool

How To Organize Hackathons & Coding Competitions

How To Organize Hackathons & Coding Competitions

Social Media: Boon Or Bane? Delving Deep Into The Debate

Social Media: Boon Or Bane? Delving Deep Into The Debate

What Is Coding | Role, Working, How To Learn & More Simplified

What Is Coding | Role, Working, How To Learn & More Simplified

IMAGES

  1. Bitwise Operators in C Language ( |, &, ~, , ^ Operators )

    bitwise or assignment operator in c

  2. Programming in C

    bitwise or assignment operator in c

  3. Bitwise Operators in C Interview MCQ

    bitwise or assignment operator in c

  4. Bitwise Operators Truth Table

    bitwise or assignment operator in c

  5. Bitwise operators in C/C++, AND, OR, XOR, left shift, right shift

    bitwise or assignment operator in c

  6. Operators In C Logicmojo

    bitwise or assignment operator in c

COMMENTS

  1. Bitwise Operators in C

    The below example demonstrates the use of bitwise NOT operator. C // C program to demonstrate the use of bitwise NOT operator. #include <stdio.h> int main {unsigned int x = 1; printf ... Assignment operators are used for assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the ...

  2. C Bitwise OR and assignment operator

    The Bitwise OR and assignment operator (|=) assigns the first operand a value equal to the result of Bitwise OR operation of two operands. (x |= y) is equivalent to (x = x | y) The Bitwise OR operator (|) is a binary operator which takes two bit patterns of equal length and performs the logical OR operation on each pair of corresponding bits.

  3. Bitwise OR Operator (|) in Programming

    Applications of Bitwise OR Operator: The bitwise OR operator finds applications in various programming scenarios, including: Bitwise Operations: Similar to other bitwise operators, the bitwise OR operator is extensively used in bitwise operations, where individual bits of binary numbers need to be manipulated.; Setting or Enabling Flags: In software development, bitwise OR operations are ...

  4. Bitwise operations in C

    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. [6] The bitwise assignment operators are as follows.

  5. C Bitwise Operators: AND, OR, XOR, Complement and Shift Operations

    Bitwise Complement Operator ~ Bitwise complement operator is a unary operator (works on only one operand). It changes 1 to 0 and 0 to 1. It is denoted by ~. 35 = 00100011 (In Binary) Bitwise complement Operation of 35 ~ 00100011 _____ 11011100 = 220 (In decimal) Twist in Bitwise Complement Operator in C Programming

  6. Complete Reference for Bitwise Operators in Programming/Coding

    Assignment operators in programming are symbols used to assign values to variables. They offer shorthand notations for performing arithmetic operations and updating variable values in a single step. ... Pre-requisite:Bitwise Operators in C/ C++Bitwise Operators in Java The bitwise complement operator is a unary operator (works on only one ...

  7. Assignment Operators in C

    Assignment Operators in C - In C language, the assignment operator stores a certain value in an already declared variable. A variable in C can be assigned the value in the form of a literal, another variable, or an expression. ... Bitwise AND assignment operator. C &= 2 is same as C = C & 2 ^= Bitwise exclusive OR and assignment operator.

  8. Assignment Operators In C Explained With Proper Code Examples ...

    Bitwise OR Assignment Operator In C (|=) The bitwise OR assignment operator (|=) performs a bitwise OR operation between the current value of the variable on the left and the value on the right and then assigns the result to the variable on the left. The example below showcases the use this compound assingment operator in C.

  9. Bitwise Operators in C: AND, OR, XOR, Shifting, and Bit Masks

    Introduction to Bitwise Operators. Overview. Bitwise operators allow direct manipulation of individual bits within data. They are crucial for systems programming, hardware interfacing, and performance optimizations. In this tutorial, we will explore bitwise operations such as AND, OR, XOR, NOT, bit shifting, and how to use bit masks through ...

  10. Bitwise Operators in C

    The bitwise operator cannot be used with primitive data types such as float, double, and so on. Because of its compatibility, the bitwise operator is mostly used with integer data types. The six main types of bitwise operators are bitwise AND, bitwise OR, bitwise exclusive OR, unary operator, left shift operator, and right shift operator.