Tags down


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 : facebook icon twitter icon

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);


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
Related Posts Related Posts :
  • How is malloc() than free() works
  • strcpy() in c gives me segmentation fault
  • What is the best practice when declaring a user defined array?
  • Efficient Assembly multiplication
  • while (getchar != '\n'); alternative to clear the buffer
  • How to fill a number with a variable amount of 0s?
  • Why C programming gives different output?
  • How to get float value from uint32_t?
  • What is the output of expression c=a+++b? If a=2,b=5 in c language...?
  • Why does it print 1 at the end?
  • Caesar cipher for numbers in C
  • Could someone explain the method of making rules for connect 4 game in C?
  • Is it a memory leak in C when code fails to free memory, but the OS will anyway?
  • Why can't I initialize a loop with i = -1?
  • How does the fork() function behave in if statements?
  • Why strcpy_s is safer than strcpy?
  • gcc flags to disable arithmetic optimisations
  • Recursion in c to print characters
  • Character array initialization using for loop
  • C Programming - Cannot Call Function More Than Once
  • Structures and pointer to pointer element
  • Error when initializing struct members in struct definition
  • Is that fixed-size array? a[]
  • It is not outputting what was expected and am unsure as to the issue
  • i want use C shell code to make buffer overflow and execute bin/sh
  • what the main function in this program returns to os?
  • Suffixes for integer types
  • I don't understand why a equals 1 and b equals 0 in this program
  • C prints other integer number than what it shows on Watches. Why is that?
  • Output of the for loop?
  • How to find 64-bit divisions in existing C code?
  • Finding a numbers with the largest/smallest number of bits set
  • how does " for (int j = 0; j < n || !putchar('\n'); j++) " work
  • handle ctrl+c in client-server socket in c programing
  • Restrict a process to read "/etc/resolv.conf" file in linux
  • Restrict scope of global variable inside one file in C
  • Arduino AES128 encryption - decryption problem
  • Why is not ampersand needed in the following code?
  • Is it possible to define a function name with #define?
  • Why is the size of the data type different when the value is directly passed to the sizeof operator?
  • Segmentation fault / Incorrect checksum triggered ocasionally when reading lines from a file
  • Adding bits at specific indexes for a uint8_t block
  • how can I use "shadow call stack" feature of clang-8 in x86_64?
  • malloc Assertion Failed
  • Array of structs inside of another struct in C
  • Why does this print the wrong part of the string?
  • Moving the cursor and printing characters in console doesn't work with sleep
  • Confusion in adding an element to a sorted list of integers
  • How I can separate integer number in 3 "houses"? Hundred, Ten and Unity
  • User-implemented memory management
  • What it entails to declare and initialize a pointer in this way: int (*p)[n]
  • Function to look though any struct in C
  • Microcontroller not recognized at USB port when connected via pins
  • Unexpected bitshift behavior in C
  • Model 3D spring from points
  • C control reaches end of non void function
  • Tax outputting 0
  • Gtk 3.0 Text View Resizable
  • Statically linking libc into my binary causes it to segfault
  • Child Process Executing Print Statement But Nothing Afterwards
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk