• Skip to main content
  • Skip to search
  • Skip to select language
  • Sign up for free

Left shift assignment (<<=)

The left shift assignment ( <<= ) operator performs left shift on the two operands and assigns the result to the left operand.

Description

x <<= y is equivalent to x = x << y , except that the expression x is only evaluated once.

Using left shift assignment

Specifications.

Specification

Browser compatibility

desktopmobileserver
)
  • Assignment operators in the JS guide
  • Left shift ( << )
  • 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

Facebook Page

  • 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 - left shift operator assignment

The Bitwise left shift assignment operator (<<=) assigns the first operand a value equal to the result of Bitwise left shift operation of two operands.

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

The Bitwise left shift operator (<<) takes the two numbers and left shift the bits of first operand by number of place specified by second operand. For example: for left shifting the bits of x by y places, the expression ( x<<y ) can be used. It is equivalent to multiplying x by 2 y .

The example below describes how left shift operator works:

The code of using left shift operator (<<) is given below:

The output of the above code will be:

Example: Count number of 1 Bits in a positive integer

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:

Bitwise AND operation with 1 at any bit results into 1 if the bit is 1 or 0 if the bit is 0. Performing such operation at every bit, and counting the number of 1 gives the count of 1 bits in the given positive integer. To achieve this bitwise left shift assignment operator can be used as shown in the example below:

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
  • TutorialKart
  • SAP Tutorials
  • Salesforce Admin
  • Salesforce Developer
  • Visualforce
  • Informatica
  • Kafka Tutorial
  • Spark Tutorial
  • Tomcat Tutorial
  • Python Tkinter

Programming

  • Bash Script
  • Julia Tutorial
  • CouchDB Tutorial
  • MongoDB Tutorial
  • PostgreSQL Tutorial
  • Android Compose
  • Flutter Tutorial
  • Kotlin Android

Web & Server

  • Selenium Java
  • Java Basics
  • Java Tutorial
  • Java HelloWorld Program
  • Java Program Structure
  • Java Datatypes
  • Java Variable Types
  • Java Access Modifiers
  • Java Operators
  • Java Decision Making
  • Print array
  • Initialize array
  • Array of integers
  • Array of strings
  • Array of objects
  • Array of arrays
  • Iterate over array
  • Array For loop
  • Array while loop
  • Append element to array
  • Check if array is empty
  • Array average
  • Check if array contains
  • Array ForEach
  • Array - Find Index of Item
  • Concatenate arrays
  • Find smallest number in array
  • Find largest number in array
  • Array reverse
  • Classes and Objects
  • Inheritance
  • Polymorphism
  • Method Overloading
  • Method Overriding/
  • Abstraction
  • Abstract methods and classes
  • Encapsulation
  • Print string
  • Read string from console
  • Create string from Char array
  • Create string from Byte array
  • Concatenate two strings
  • Get index of the first Occurrence of substring
  • Get index of nth occurrence of substring
  • Check if two strings are equal
  • Check if string ends with specific suffix
  • Check if string starts with specific prefix
  • Check if string is blank
  • Check if string is empty
  • Check if string contains search substring
  • Validate if string is a Phone Number
  • Character Level
  • Get character at specific index in string
  • Get first character in string
  • Get last character from string
  • Transformations
  • Replace first occurrence of string
  • Replace all occurrences of a string
  • Join strings
  • Join strings in string array
  • Join strings in ArrayList
  • Reverse a string
  • Trim string
  • Split string
  • Remove whitespaces in string
  • Replace multiple spaces with single space
  • Comparisons
  • Compare strings lexicographically
  • Compare String and CharSequence
  • Compare String and StringBuffer
  • Java Exception Handling StringIndexOutOfBoundsException
  • Convert string to int
  • Convert string to float
  • Convert string to double
  • Convert string to long
  • Convert string to boolean
  • Convert int to string
  • Convert int to float
  • Convert int to double
  • Convert int to long
  • Convert int to char
  • Convert float to string
  • Convert float to int
  • Convert float to double
  • Convert float to long
  • Convert long to string
  • Convert long to float
  • Convert long to double
  • Convert long to int
  • Convert double to string
  • Convert double to float
  • Convert double to int
  • Convert double to long
  • Convert char to int
  • Convert boolean to string
  • Create a file
  • Read file as string
  • Write string to file
  • Delete File
  • Rename File
  • Download File from URL
  • Replace a String in File
  • Filter list of files or directories
  • Check if file is readable
  • Check if file is writable
  • Check if file is executable
  • Read contents of a file line by line using BufferedReader
  • Read contents of a File line by line using Stream
  • Check if n is positive or negative
  • Read integer from console
  • Add two integers
  • Count digits in number
  • Largest of three numbers
  • Smallest of three numbers
  • Even numbers
  • Odd numbers
  • Reverse a number
  • Prime Number
  • Print All Prime Numbers
  • Factors of a Number
  • Check Palindrome number
  • Check Palindrome string
  • Swap two numbers
  • Even or Odd number
  • Java Classes
  • ArrayList add()
  • ArrayList addAll()
  • ArrayList clear()
  • ArrayList clone()
  • ArrayList contains()
  • ArrayList ensureCapacity()
  • ArrayList forEach()
  • ArrayList get()
  • ArrayList indexOf()
  • ArrayList isEmpty()
  • ArrayList iterator()
  • ArrayList lastIndexOf()
  • ArrayList listIterator()
  • ArrayList remove()
  • ArrayList removeAll()
  • ArrayList removeIf()
  • ArrayList removeRange()
  • ArrayList retainAll()
  • ArrayList set()
  • ArrayList size()
  • ArrayList spliterator()
  • ArrayList subList()
  • ArrayList toArray()
  • ArrayList trimToSize()
  • HashMap clear()
  • HashMap clone()
  • HashMap compute()
  • HashMap computeIfAbsent()
  • HashMap computeIfPresent()
  • HashMap containsKey()
  • HashMap containsValue()
  • HashMap entrySet()
  • HashMap get()
  • HashMap isEmpty()
  • HashMap keySet()
  • HashMap merge()
  • HashMap put()
  • HashMap putAll()
  • HashMap remove()
  • HashMap size()
  • HashMap values()
  • HashSet add()
  • HashSet clear()
  • HashSet clone()
  • HashSet contains()
  • HashSet isEmpty()
  • HashSet iterator()
  • HashSet remove()
  • HashSet size()
  • HashSet spliterator()
  • Integer bitCount()
  • Integer byteValue()
  • Integer compare()
  • Integer compareTo()
  • Integer compareUnsigned()
  • Integer decode()
  • Integer divideUnsigned()
  • Integer doubleValue()
  • Integer equals()
  • Integer floatValue()
  • Integer getInteger()
  • Integer hashCode()
  • Integer highestOneBit()
  • Integer intValue()
  • Integer longValue()
  • Integer lowestOneBit()
  • Integer max()
  • Integer min()
  • Integer numberOfLeadingZeros()
  • Integer numberOfTrailingZeros()
  • Integer parseInt()
  • Integer parseUnsignedInt()
  • Integer remainderUnsigned()
  • Integer reverse()
  • Integer reverseBytes()
  • Integer rotateLeft()
  • Integer rotateRight()
  • Integer shortValue()
  • Integer signum()
  • Integer sum()
  • Integer toBinaryString()
  • Integer toHexString()
  • Integer toOctalString()
  • Integer toString()
  • Integer toUnsignedLong()
  • Integer toUnsignedString()
  • Integer valueOf()
  • StringBuilder append()
  • StringBuilder appendCodePoint()
  • StringBuilder capacity()
  • StringBuilder charAt()
  • StringBuilder chars()
  • StringBuilder codePointAt()
  • StringBuilder codePointBefore()
  • StringBuilder codePointCount()
  • StringBuilder codePoints()
  • StringBuilder delete()
  • StringBuilder deleteCharAt()
  • StringBuilder ensureCapacity()
  • StringBuilder getChars()
  • StringBuilder indexOf()
  • StringBuilder insert()
  • StringBuilder lastIndexOf()
  • StringBuilder length()
  • StringBuilder offsetByCodePoints()
  • StringBuilder replace()
  • StringBuilder reverse()
  • StringBuilder setCharAt()
  • StringBuilder setLength()
  • StringBuilder subSequence()
  • StringBuilder substring()
  • StringBuilder toString()
  • StringBuilder trimToSize()
  • Arrays.asList()
  • Arrays.binarySearch()
  • Arrays.copyOf()
  • Arrays.copyOfRange()
  • Arrays.deepEquals()
  • Arrays.deepToString()
  • Arrays.equals()
  • Arrays.fill()
  • Arrays.hashCode()
  • Arrays.sort()
  • Arrays.toString()
  • Random doubles()
  • Random ints()
  • Random longs()
  • Random next()
  • Random nextBoolean()
  • Random nextBytes()
  • Random nextDouble()
  • Random nextFloat()
  • Random nextGaussian()
  • Random nextInt()
  • Random nextLong()
  • Random setSeed()
  • Math random
  • Math signum
  • Math toDegrees
  • Math toRadians
  • Java Date & Time
  • ❯ Java Tutorial

Java Left-shift Assignment (<<=) Operator

Java left-shift assignment.

In Java, Left-shift Assignment Operator is used to left shift the value in a variable (left operand) by a value (right operand) and assign the result back to this variable (left operand).

In this tutorial, we will learn how to use Left-shift Assignment operator in Java, with examples.

The syntax to left shift a value in variable x by 2 places and assign the result to x using Left-shift Assignment Operator is

In the following example, we take a variable x with an initial value of 5 , add left shift by 2 places, and assign the result to x , using Left-shift Assignment Operator.

In this Java Tutorial , we learned about Left-shift Assignment Operator in Java, with examples.

Popular Courses by TutorialKart

App developement, web development, online tools.

  |     |        
  

.

.

The (<<=) operator is used to shift the bits of an expression to the left.

The operator syntax has these parts:

operator is exactly the same as specifying:

The <<= operator shifts the bits of result left by the number of bits specified in expression . For example:

The variable temp has a value of 56 because 14 (00001110 in binary) shifted left two bits equals 56 (00111000 in binary). Bits are filled in with zeroes when shifting.

For information on when a run-time error is generated by the <<= operator, see the Operator Behavior table.

See also: << Operator , >> Operator , >>> Operator , Operator Behavior , Operator Precedence , Operator Summary

C Point Pty Ltd. All Rights Reserved.

TutorialsTonight Logo

JAVASCRIPT ASSIGNMENT OPERATORS

In this tutorial, you will learn about all the different assignment operators in javascript and how to use them in javascript.

Assignment Operators

In javascript, there are 16 different assignment operators that are used to assign value to the variable. It is shorthand of other operators which is recommended to use.

The assignment operators are used to assign value based on the right operand to its left operand.

The left operand must be a variable while the right operand may be a variable, number, boolean, string, expression, object, or combination of any other.

One of the most basic assignment operators is equal = , which is used to directly assign a value.

javascript assignment operator

Assignment Operators List

Here is the list of all assignment operators in JavaScript:

In the following table if variable a is not defined then assume it to be 10.

Operator Description Example Equivalent to
= a = 10 a = 10
+= a += 10 a = a + 10
-= a -= 10 a = a - 10
*= a *= 10 a = a * 10
/= a /= 10 a = a / 10
%= a %= 10 a = a % 10
**= a **= 2 a = a ** 2
<<= a <<= 1 a = a << 1
>>= a >>= 2 a = a >> 2
>>>= a >>>= 1 a = a >>> 1
&= a &= 4 a = a & 4
|= a |= 2 a = a | 2
^= a ^= 5 a = a ^ 5
&&= a &&= 3 a = a && 3
||= a ||= 4 a = a || 4
??= a ??= 2 a = a ?? 2

Assignment operator

The assignment operator = is the simplest value assigning operator which assigns a given value to a variable.

The assignment operators support chaining, which means you can assign a single value in multiple variables in a single line.

Addition assignment operator

The addition assignment operator += is used to add the value of the right operand to the value of the left operand and assigns the result to the left operand.

On the basis of the data type of variable, the addition assignment operator may add or concatenate the variables.

Subtraction assignment operator

The subtraction assignment operator -= subtracts the value of the right operand from the value of the left operand and assigns the result to the left operand.

If the value can not be subtracted then it results in a NaN .

Multiplication assignment operator

The multiplication assignment operator *= assigns the result to the left operand after multiplying values of the left and right operand.

Division assignment operator

The division assignment operator /= divides the value of the left operand by the value of the right operand and assigns the result to the left operand.

Remainder assignment operator

The remainder assignment operator %= assigns the remainder to the left operand after dividing the value of the left operand by the value of the right operand.

Exponentiation assignment operator

The exponential assignment operator **= assigns the result of exponentiation to the left operand after exponentiating the value of the left operand by the value of the right operand.

Left shift assignment

The left shift assignment operator <<= assigns the result of the left shift to the left operand after shifting the value of the left operand by the value of the right operand.

Right shift assignment

The right shift assignment operator >>= assigns the result of the right shift to the left operand after shifting the value of the left operand by the value of the right operand.

Unsigned right shift assignment

The unsigned right shift assignment operator >>>= assigns the result of the unsigned right shift to the left operand after shifting the value of the left operand by the value of the right operand.

Bitwise AND assignment

The bitwise AND assignment operator &= assigns the result of bitwise AND to the left operand after ANDing the value of the left operand by the value of the right operand.

Bitwise OR assignment

The bitwise OR assignment operator |= assigns the result of bitwise OR to the left operand after ORing the value of left operand by the value of the right operand.

Bitwise XOR assignment

The bitwise XOR assignment operator ^= assigns the result of bitwise XOR to the left operand after XORing the value of the left operand by the value of the right operand.

Logical AND assignment

The logical AND assignment operator &&= assigns value to left operand only when it is truthy .

Note : A truthy value is a value that is considered true when encountered in a boolean context.

Logical OR assignment

The logical OR assignment operator ||= assigns value to left operand only when it is falsy .

Note : A falsy value is a value that is considered false when encountered in a boolean context.

Logical nullish assignment

The logical nullish assignment operator ??= assigns value to left operand only when it is nullish ( null or undefined ).

EDUCBA

Left Shift Operator in C

Priya Pedamkar

Updated April 10, 2023

Left Shift Operator in C

Introduction to Left Shift Operator in C

Left shift operator is a bitwise shift operator in C which operates on bits. It is a binary operator which means it requires two operands to work on. Following are some important points regarding Left shift operator in C:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • It is represented by ‘<<’ sign.
  • It is used to shift the bits of a value to the left by adding zeroes to the empty spaces created at the right side after shifting.
  • The bits of first operand are shifted to the left by the number of positions specified by the second operand.

The syntax for left shift operator in C is as follows:

In the above statement, there are two values; the first one is an integer variable on which we want to apply left shift operator. The name of this variable can be any name given by the user. The second value is a number which specifies the number of positions a user wants to shift the bits to the left.

How Left Shift Operator Works in C?

Left shift operator requires two operands to work on. Both the operands of the left shift operator should be of integral type. It shifts the bits of the first operand to the left by the number of positions specified by the second operand. Simultaneously, the empty spaces created by the bits shifted to the left are then filled with zeroes.Including the sign bit, the bits which are shifted off to the end are then discarded.

Following are some cases when the result of left shift operation will be undefined:

  • If the value of first operand is negative, then the result of left shift operation will be undefined.
  • Similarly, if the value of second operand is negative or if it is greater than or equal to the number of bits in the first operand, then the result of left shift operation will be undefined.
  • The result of left shift operation will also be undefined if the value of second operand is greater than the size of integer.

Thus, left shift operation will be performed when both the operands are positive.

On the other hand, if the value of second operand is zero then left shift operation will not be performed. Such as the result of 40<<0 will be equal to 40 itself.

Let us now understand the working of left shift operator with the help of an example. In this example we will take a decimal number say 40. The binary equivalent of 40 is 101000. We will perform left shift operation on this binary value.

Left Shift Operator in C

In the above example, we can see that on performing left shift operation on a binary value all its bits have been shifted to the left and the empty space created on the right side is filled with zero.

Thus, the value of 40<<1 is 01010000. The decimal equivalent of this binary value is 80.

Examples to Implement Left Shift Operator in C

Below are the examples of Left Shift Operator in C:

Example showing left shift operation performed on two positive operands.

Left shift operator in C Example 1

Example showing a scenario when the value of second operand is negative.

Left shift operator in C Example 2

Along with this there is a warning in the program for the line highlighted in yellow.

Below is the Warning:

Warning Example 2

We got this warning because our second operand is negative.

Example showing scenario when the value of first operand is negative.

Left shift operator in C Example 4

We got this warning because our first operand is negative.

Example showing scenarios when number of positions to be shifted is zero and greater than the size of integer.

Left shift operator in C Example 6

Along with this, there is a warning in the program for line highlighted in yellow.

Warning Example 4

We got this warning because the size of the operand is greater than the size of an integer.

  • The left shift operator is a logical bitwise operator.
  • It is a binary operator that operates on two positive integral operands.
  • It shifts the bits to the left by the number of positions specified by its second operand.
  • Empty spaces created in the right are filled with zeroes.

Recommended Articles

This is a guide to Left Shift Operator in C. Here we discuss the Introduction of Left Shift Operator in C and how it works along with different Examples and its Code Implementation. You can also go through our other suggested articles to learn more –

  • Prime Numbers in C (Examples)
  • How to Reverse Number in C?
  • Introduction to Reverse String in C
  • Reverse String in PHP | Loops

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy .

Forgot Password?

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Quiz

Explore 1000+ varieties of Mock tests View more

Submit Next Question

Early-Bird Offer: ENROLL NOW

quiz

  • Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
  • Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand
  • OverflowAI GenAI features for Teams
  • OverflowAPI Train & fine-tune LLMs
  • Labs The future of collective knowledge sharing
  • About the company Visit the blog

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Get early access and see previews of new features.

What is the difference between directly assigning the result of left shift operation to a variable and the left shift assignment operation in C?

In the following expression, the result of the left shift operation is assigned to the variable i .

In the following expression, the left shift assignment operation is carried.

Both the above expressions gave different results. But it's not the same with the following two expressions. Both of them gave the same result. So what could be the reason for the above expressions to return different values?

  • relational-operators

Peter Mortensen's user avatar

  • 5 How many bits does your compiler use for the int data type? –  paddy Commented May 30, 2017 at 6:42
  • 5 Try shifting by 2 bits instead of 32 and see the result! –  Swanand Commented May 30, 2017 at 6:44
  • 19 Hint: compile with all warnings enabled. –  paddy Commented May 30, 2017 at 6:46
  • Those blocks of code are not expressions. They're sets of statements. –  jpmc26 Commented May 30, 2017 at 9:04
  • Duplicate : stackoverflow.com/questions/7401888/… –  alinsoar Commented May 31, 2017 at 7:13

3 Answers 3

The C standard says:

The result is undefined if the right operand is negative, or greater than or equal to the number of bits in the left expression’s type.

So, it is undefined behavior because int is normally 32 bits in size, which means that only 0 through 31 steps are well-defined .

msc's user avatar

  • 1 31 steps is also undefined unless input was zero (shifting 1 into sign bit) –  M.M Commented May 30, 2017 at 6:49
  • 1 "because size of int normally 32 bits": that depends on the compiler. –  glglgl Commented May 30, 2017 at 7:03
  • 1 @M.M No, 1's dropping off on the left side is ok. It's really only a shift by more than (or equal) the number of bits in the underlying type that's undefined, regardless of the actual values of those bits. –  Marc Schütz Commented May 30, 2017 at 10:30
  • 1 @M.M ISO99 6.5.7: says it's well-defined for unsigned types. It does state that it's undefined for signed integers, but IMO that's just an instance of the signed overflow rule, not specific to left shifts. –  Marc Schütz Commented May 30, 2017 at 11:05
  • 1 @MarcSchütz it still applies regardless of how you want to categorize it - assuming 32-bit int, 1 << 31 , 7 << 29 etc. are undefined behaviour, and an easy way to remember the rules is that trying to left-shift a 1 into the sign-bit is always UB –  M.M Commented May 30, 2017 at 11:12

I agree with Cody Gray's comments . Just for people in future who end up here, the way to resolve this ambiguity is using unsigned long long.

Shuvam's user avatar

  • In this case the situation is the same for 64 bits shift ? –  alinsoar Commented May 30, 2017 at 10:03
  • Any magic number in code is usually word length unless otherwise typecasted. In this case, as you are trying to right shift 32bits i.e equal to the size of word length, we need to tell the compiler not to treat it as one word width data (32bit). –  Shuvam Commented May 30, 2017 at 10:23
  • 3 Probably diligent when thinking about shifting by n bits to recommend the use of int64_t , uint64_t , etc from stdint that explicitly ensure bit length –  cat Commented May 30, 2017 at 15:21
  • 1 Yes +1 and reiterating: ALWAYS use the stdint types when you are making assumptions about the number of bits. –  paddy Commented May 30, 2017 at 23:32
  • @paddy stdint is applicable only when you have to access to the standard library! Sometimes (like in embedded) you have to write your own lib. So, I don't think "ALWAYS" holds good here! –  Shuvam Commented Jun 8, 2017 at 6:20

The abstract operational semantics from ISO/IEC 9899 says:

3 .... ... . If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.

In your case, disassembling and seeing what happens, we see so:

The generated code tries indeed to shift, but the shll %cl,-0x4(%rbp) (shift left of a long) has no effect.

The undefined behaviour in this case lies in assembly, namely in SHL operation.

alinsoar's user avatar

  • 3 "The undefined behaviour in this case lies in assembly, namely in SHL operation." This is incorrect. Shifting by an excess of bits is well-defined on x86, as you can see in the documentation . If you were writing the code in assembly, there would be no problem. The reason it is undefined behavior is because of the requirements set out in the C language standard. "...the shll %cl,-0x4(%rbp) (shift left of a long) has no effect." I have no idea why you say that. It absolutely does have an effect. –  Cody Gray ♦ Commented May 30, 2017 at 8:20
  • 1 Technically, shll %cl, -0x4(%rbp) does modify the value on the stack. Why wouldn't it? The instruction says to shift the value at an offset of -4 from rbp by the value in cl , and that's precisely what it does. Now, the x86 ISA is specifically documented as masking shift counts for 32-bit operands to 5 bits, which (A) is what makes this well-defined behavior, and (B) means that shifting a value by 32 is essentially an identity operation in that it evaluates the original value. So in that sense, no, the value at -0x4(%rbp) isn't modified, but it isn't supposed to be. –  Cody Gray ♦ Commented May 30, 2017 at 8:42
  • 1 It is difficult for me to understand the point that you are making. It is very clear from the documentation that shifting by an excess of bits is not undefined behavior on x86; in fact, it is quite well-defined so we can predict exactly what the result will be of shifting 7 by 32. There is no "problem" in the assembly. The problem lies in the C source code that the compiler is translating to machine code, and the C source code is invalid because it exhibits undefined behavior. The top half of the answer is correct; the bottom half is misleading or just downright wrong. –  Cody Gray ♦ Commented May 30, 2017 at 9:38
  • 1 To put it another way, if a human wrote this program by hand in x86 assembly language, we might say that it has a bug if the human meant the left shift to do something other than what it does, but we wouldn't say that it had undefined behavior, because all of the machine instructions have well-defined behavior. (We might not like that Intel specified shll to reduce the shift count mod 32, but they did specify it.) However, the C program that got translated into this assembly dump does have undefined behavior, because the C standard doesn't specify what an over-wide shift count does. –  zwol Commented May 30, 2017 at 16:16
  • 2 @dan I think you are misunderstanding my point, as I'm afraid that alinsoar was. I fully agree that this is undefined behavior in C, and it doesn't make it any less undefined just because one particular compiler when targeting one particular architecture seems to consistently translate the code into a particular machine instruction. My issue was with the claim made in the answer that "The undefined behaviour in this case lies in assembly, namely in SHL operation." This is wrong, the behavior is not undefined in assembly language, it is undefined in C . –  Cody Gray ♦ Commented May 31, 2017 at 7:01

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Sign up or log in

Post as a guest.

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .

  • The Overflow Blog
  • Where does Postgres fit in a world of GenAI and vector databases?
  • Mobile Observability: monitoring performance through cracked screens, old...
  • Featured on Meta
  • Announcing a change to the data-dump process
  • Bringing clarity to status tag usage on meta sites
  • What does a new user need in a homepage experience on Stack Overflow?
  • Staging Ground Reviewer Motivation
  • Feedback requested: How do you use tag hover descriptions for curating and do...

Hot Network Questions

  • 3D printed teffilin?
  • Rings demanding identity in the categorical context
  • Stuck on Sokoban
  • Do the amplitude and frequency of gravitational waves emitted by binary stars change as the stars get closer together?
  • How do you end-punctuate quotes when the entire quote is used as a noun phrase?
  • I overstayed 90 days in Switzerland. I have EU residency and never got any stamps in passport. Can I exit/enter at airport without trouble?
  • Where does the energy in ion propulsion come from?
  • If the Hom-space of finite length modules is generated by single elements, must the elements be conjugate?
  • Is it possible to accurately describe something without describing the rest of the universe?
  • AM-GM inequality (but equality cannot be attained)
  • How to disable Google Lens in search when using Google Chrome?
  • Dutch Public Transportation Electronic Payment Options
  • "TSA regulations state that travellers are allowed one personal item and one carry on"?
  • What does "seeing from one end of the world to the other" mean?
  • Whence “uniform distribution”?
  • Memory-optimizing arduino code to be able to print all files from SD card
  • Add colored points to QGIS from CSV file of latitude and longitude
  • What explanations can be offered for the extreme see-sawing in Montana's senate race polling?
  • Encode a VarInt
  • What's the average flight distance on a typical single engine aircraft?
  • Is it possible to have a planet that's gaslike in some areas and rocky in others?
  • Why was this lighting fixture smoking? What do I do about it?
  • Why is the movie titled "Sweet Smell of Success"?
  • Has a tire ever exploded inside the Wheel Well?

left shift assignment

01 Career Opportunities

02 beginner, 03 intermediate, 04 advanced, 05 training programs, c programming assignment operators, free c programming online course with certificate, what is an assignment operator in c, types of assignment operators in c.

1. Simple Assignment Operator (=)

Example of simple assignment operator.

2. Compound Assignment Operators

+=addition assignmentIt adds the right operand to the left operand and assigns the result to the left operand.
-=subtraction assignmentIt subtracts the right operand from the left operand and assigns the result to the left operand.
*=multiplication assignmentIt multiplies the right operand with the left operand and assigns the result to the left operand
/=division assignmentIt divides the left operand with the right operand and assigns the result to the left operand.
%=modulo assignmentIt takes modulus using two operands and assigns the result to the left operand.

Example of Augmented Arithmetic and Assignment Operators

&=bitwise AND assignmentIt performs the bitwise AND operation on the variable with the value on the right
|=bitwise OR assignmentIt performs the bitwise OR operation on the variable with the value on the right
^=bitwise XOR assignmentIt performs the bitwise XOR operation on the variable with the value on the right
<<=bitwise left shift assignmentShifts the bits of the variable to the left by the value on the right
>>=bitwise right shift assignmentShifts the bits of the variable to the right by the value on the right

Example of Augmented Bitwise and Assignment Operators

Practice problems on assignment operators in c, 1. what will the value of "x" be after the execution of the following code, 2. after executing the following code, what is the value of the number variable, benefits of using assignment operators, best practices and tips for using the assignment operator, live classes schedule.

Filling Fast
Filling Fast
Filling Fast
Filling Fast
Filling Fast
Filling Fast

About Author

  • C++ Data Types
  • C++ Input/Output
  • C++ Pointers
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management

Assignment Operators In C++

In C++, the assignment operator forms the backbone of many algorithms and computational processes by performing a simple operation like assigning a value to a variable. It is denoted by equal sign ( = ) and provides one of the most basic operations in any programming language that is used to assign some value to the variables in C++ or in other words, it is used to store some kind of information.

The right-hand side value will be assigned to the variable on the left-hand side. The variable and the value should be of the same data type.

The value can be a literal or another variable of the same data type.

 

Compound Assignment Operators

In C++, the assignment operator can be combined into a single operator with some other operators to perform a combination of two operations in one single statement. These operators are called Compound Assignment Operators. There are 10 compound assignment operators in C++:

  • Addition Assignment Operator ( += )
  • Subtraction Assignment Operator ( -= )
  • Multiplication Assignment Operator ( *= )
  • Division Assignment Operator ( /= )
  • Modulus Assignment Operator ( %= )
  • Bitwise AND Assignment Operator ( &= )
  • Bitwise OR Assignment Operator ( |= )
  • Bitwise XOR Assignment Operator ( ^= )
  • Left Shift Assignment Operator ( <<= )
  • Right Shift Assignment Operator ( >>= )

Lets see each of them in detail.

1. Addition Assignment Operator (+=)

In C++, the addition assignment operator (+=) combines the addition operation with the variable assignment allowing you to increment the value of variable by a specified expression in a concise and efficient way.

This above expression is equivalent to the expression:

   

2. Subtraction Assignment Operator (-=)

The subtraction assignment operator (-=) in C++ enables you to update the value of the variable by subtracting another value from it. This operator is especially useful when you need to perform subtraction and store the result back in the same variable.

   

3. Multiplication Assignment Operator (*=)

In C++, the multiplication assignment operator (*=) is used to update the value of the variable by multiplying it with another value.

 

4. Division Assignment Operator (/=)

The division assignment operator divides the variable on the left by the value on the right and assigns the result to the variable on the left.

       

5. Modulus Assignment Operator (%=)

The modulus assignment operator calculates the remainder when the variable on the left is divided by the value or variable on the right and assigns the result to the variable on the left.

     

6. Bitwise AND Assignment Operator (&=)

This operator performs a bitwise AND between the variable on the left and the value on the right and assigns the result to the variable on the left.

   

7. Bitwise OR Assignment Operator (|=)

The bitwise OR assignment operator performs a bitwise OR between the variable on the left and the value or variable on the right and assigns the result to the variable on the left.

8. Bitwise XOR Assignment Operator (^=)

The bitwise XOR assignment operator performs a bitwise XOR between the variable on the left and the value or variable on the right and assigns the result to the variable on the left.

9. Left Shift Assignment Operator (<<=)

The left shift assignment operator shifts the bits of the variable on the left to left by the number of positions specified on the right and assigns the result to the variable on the left.

10. Right Shift Assignment Operator (>>=)

The right shift assignment operator shifts the bits of the variable on the left to the right by a number of positions specified on the right and assigns the result to the variable on the left.

Also, it is important to note that all of the above operators can be overloaded for custom operations with user-defined data types to perform the operations we want.

Please Login to comment...

Similar reads.

  • Geeks Premier League
  • Geeks Premier League 2023
  • SUMIF in Google Sheets with formula examples
  • How to Get a Free SSL Certificate
  • Best SSL Certificates Provider in India
  • Elon Musk's xAI releases Grok-2 AI assistant
  • Content Improvement League 2024: From Good To A Great Article

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

IMAGES

  1. Left Shift Operator in C

    left shift assignment

  2. LEFT SHIFT ASSIGNMENT OPERATOR || OPERATORS IN C ||

    left shift assignment

  3. Left Shift Operator in Java

    left shift assignment

  4. Left Shift Operator in Java

    left shift assignment

  5. Java Shift Left Operator Example

    left shift assignment

  6. Left Shift Operator in C

    left shift assignment

VIDEO

  1. #90: Left shift assignment Operator

  2. Shift assessment video assignment

  3. The Tech Talk

  4. NMRFAM-SPARKY: Chemical shift reference view

  5. Shift Cipher assignment program in Java

  6. Shift Assignment

COMMENTS

  1. Left shift assignment (<<=)

    Left shift assignment (<<=) The left shift assignment ( <<=) operator performs left shift on the two operands and assigns the result to the left operand. x <<= y is equivalent to x = x << y, except that the expression x is only evaluated once.

  2. Left Shift Assignment (<<=) Operator in JavaScript

    The Left Shift Assignment Operator is represented by "<<=". This operator moves the specified number of bits to the left and assigns that result to the variable. We can fill the vacated place by 0. The left shift operator treats the integer stored in the variable to the operator's left as a 32-bit binary number. This can also be explained ...

  3. Left Shift and Right Shift Operators in C/C++

    The left shift (<<) is a binary operator that takes two numbers, left shifts the bits of the first operand, and the second operand decides the number of places to shift. In other words, left-shifting an integer "a" with an integer "b" denoted as '(a<<b)' is equivalent to multiplying a with 2^b (2 raised to power b). Syntax.

  4. What are bitwise shift (bit-shift) operators and how do they work?

    For each shift left, the high-order bit is shifted out (and ignored/lost), and a zero is brought in on the right. This means that when a left shift is applied to 32-bit compiler, bits are lost once they are shifted past bit position 31. If the compiler is of 64-bit then bits are lost after bit position 63.

  5. Left shift assignment (<<=)

    The left shift assignment (<<=) operator performs left shift on the two operands and assigns the result to the left operand.

  6. C left shift operator assignment

    The Bitwise left shift assignment operator (<<=) assigns the first operand a value equal to the result of Bitwise left shift operation of two operands. The Bitwise left shift operator (<<) takes the two numbers and left shift the bits of first operand by number of place specified by second operand. For example: for left shifting the bits of x ...

  7. Left Shift ( ) Bitwise Operator in JavaScript

    The Left Shift Assignment Operator is represented by "&lt;&lt;=". This operator moves the specified number of bits to the left and assigns that result to the variable. We can fill the vacated place by 0. The left shift operator treats the integer stored in the variable to the operator's left as a 32-bit binary number. This can also be explained as

  8. C++ Left-shift Assignment

    In C++, Left-shift Assignment Operator is used to left shift the value in a variable (left operand) by a value (right operand) and assign the result back to this variable (left operand). The syntax to left shift a value in variable x by 2 places and assign the result to x using Left-shift Assignment Operator is. x <<= 2.

  9. Java Left-shift Assignment

    In Java, Left-shift Assignment Operator is used to left shift the value in a variable (left operand) by a value (right operand) and assign the result back to this variable (left operand). In this tutorial, we will learn how to use Left-shift Assignment operator in Java, with examples. The syntax to left shift a value in variable x by 2 places ...

  10. Left Shift Assignment Operator (<<=)

    The <<= operator shifts the bits of result left by the number of bits specified in expression. For example: temp = 14. temp <<= 2. document.write (temp); To run the code above, paste it into JavaScript Editor, and click the Execute button. The variable temp has a value of 56 because 14 (00001110 in binary) shifted left two bits equals 56 ...

  11. C left shift and assignment

    By specification the left operand of the bitwise shift operator undergoes integer promotion. So: y = (x<< z); is here equivalent to: y = ((int) x << z); The result of the << expression is of the type of the promoted left operand, i. e., int and it is converted to unsigned short during assignment. answered Sep 29, 2014 at 10:13.

  12. Javascript Assignment Operators (with Examples)

    The right shift assignment operator >>= assigns the result of the right shift to the left operand after shifting the value of the left operand by the value of the right operand. Example let a = 10; let b = 3; console.log(a >>= 2); console.log(b >>= 2);

  13. C++ left shift operator assignment

    The Bitwise left shift assignment operator (<<=) assigns the first operand a value equal to the result of Bitwise left shift operation of two operands. The Bitwise left shift operator (<<) takes the two numbers and left shift the bits of first operand by number of place specified by second operand. For example: for left shifting the bits of x ...

  14. Left shift operator in C

    And after left shifting you will get a = 1424. Generally for decimal (base 10) numbers. a = a<< n is a = a*2^n a = a>> n is a = a/2^n For hexadecimal (base 16) numbers, any shift by n (left or right), can be considered, as a corresponding shift of the digits of the binary equivalent. But this depends on sizeof(int), used for a given compiler.

  15. Left Shift Operator in C

    The syntax for left shift operator in C is as follows: variable_name << number_of_positions. In the above statement, there are two values; the first one is an integer variable on which we want to apply left shift operator. The name of this variable can be any name given by the user. The second value is a number which specifies the number of ...

  16. What is the difference between directly assigning the result of left

    In the following expression, the left shift assignment operation is carried. int x = 7; x <<= 32; printf("x = %d\n",x); Both the above expressions gave different results. But it's not the same with the following two expressions. Both of them gave the same result. So what could be the reason for the above expressions to return different values?

  17. Left Shift Operator in Java

    The syntax of the left-shift operator in Java is given below, Syntax: x << n. Here, x: an integer. n: a non-negative integer. Return type: An integer after shifting x by n positions toward left. Exception: When n is negative the output is undefined. Below is the program to illustrate how we can use the left shift operator in Java.

  18. C Programming Assignment Operators

    Assignment Operators in C are used to assign values to the variables. They come under the category of binary operators as they require two operands to operate upon. The left side operand is called a variable and the right side operand is the value. The value on the right side of the "=" is assigned to the variable on the left side of "=".

  19. Right Shift Assignment(>>=) Operator in JavaScript

    The Left Shift Assignment Operator is represented by "&lt;&lt;=". This operator moves the specified number of bits to the left and assigns that result to the variable. We can fill the vacated place by 0. The left shift operator treats the integer stored in the variable to the operator's left as a 32-bit binary number. This can also be explained as

  20. Assignment Operators In C++

    Left Shift Assignment Operator ( <<= ) Right Shift Assignment Operator ( >>= ) Lets see each of them in detail. 1. Addition Assignment Operator (+=) In C++, the addition assignment operator (+=) combines the addition operation with the variable assignment allowing you to increment the value of variable by a specified expression in a concise and ...