Tags
 IOS SQL HTML C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6

# In C, what happens if we left shift the bits out of range and again right shift the values in the same operation

By : Janakiraman TK
Date : September 14 2020, 08:00 AM
I wish this help you In case of unsigned short I shifted 383 by 11 positions towards left and again in the same instruction shifted it by 15 positions right, I expected the value to be 1 but it was 27. But when I used both the shift operations in different instructions one after another(first left shift and then right), output was 1. here is a sample code :- , For clarity, you compare
code :
``````unsigned short seed1 = (seed<<11);
unsigned short seed2 = (seed1>>15);
``````
``````unsigned short seed3 = ((seed<<11)>>15);
``````
``````int seed1 = (seed<<11); // instead of short
unsigned short seed2 = (seed1>>15);
``````

Share :

## why logical right shift and arithmetic right shift have a shift range of 1- 32

By : Anu
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , It has to do with the special meanings of certain shift operations. From DDI 0029E (the ARM7TDMI datasheet):

## Find set bits and shift them to left most position using 32 bits

By : user11300
Date : March 29 2020, 07:55 AM
wish helps you I am trying to figure out how to find all set bits in a hex number and then shift those bits to the left most positions using a 32 bit notation. , What about:
code :
``````void shiftSet(unsigned long x){
//Function to shift all set bits to the left
//Precondition: x is a hexidecimal number
//Postcondition: all set bits in x are shifted to the left most bit placements

unsigned int count;
for (count = 0; x; count++)
x &= x - 1; // clear the least significant bit set

unsigned long u = ~0UL;
if (count == 0)
u = 0;
else if (count < 32)
u = (u >> (32 - count)) << (32 - count);

printBits(u);
}
``````

## What is the difference between directly assigning the result of left shift operation to a variable and the left shift as

By : fredi34
Date : March 29 2020, 07:55 AM
wish help you to fix your issue In the following expression, the result of the left shift operation is assigned to the variable i. , The C standard says:

## Size of unsigned char is 8 bits but why can I shift it (left or right) by or upto 31 bits?

By : user3003232
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , So here is my code ,
[...] that means ch is of 32 bits [...] ?
code :
``````\$ gcc -Wconversion test.c
test.c:4:13: warning: implicit conversion loses integer precision: 'int' to 'unsigned char' [-Wconversion]
ch = ch << 31;
~ ~~~^~~~~
1 warning generated.
``````

## How do C++ compilers distingush between shift bits left / shift bits right and ostream<< / ostream>> operato

By : kali
Date : March 29 2020, 07:55 AM
I hope this helps you . Operators in C++ have a precedence and an associativity.
The expression