How is shift operator evaluated in C?

chouaib

I recently noticed a (weird) behavior when I conducted operations using shift >> <<!

To explain it, let me write this small runnable code that does two operations which are supposed to be identical(In my understanding), but I'm surprised with different results!

#include <stdio.h>

int main(void) {
    unsigned char a=0x05, b=0x05;

    // first operation
    a = ((a<<7)>>7);

    // second operation
    b <<= 7;
    b >>= 7;

    printf("a=%X b=%X\n", a, b);
    return 0;
} 

When ran, a = 5 and b = 1. I expect them both to be equal to 1! Can someone kindly explain why I got such a result?

P.S: In my environment the size of unsigned char is 1 byte

starrify

The shift operations would do integer promotions to its operands, and in your code the resulting int is converted back to char like this:

// first operation
a = ((a<<7)>>7); // a = (char)((a<<7)>>7);

// second operation
b <<= 7; // b = (char) (b << 7);
b >>= 7; // b = (char) (b >> 7);

Quote from the N1570 draft (which became the standard of C11 later):

6.5.7 Bitwise shift operators:

  1. Each of the operands shall have integer type.
  2. The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. 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.

And it's supposed that in C99 and C90 there are similar statements.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Shift right zero fill operator >>> in Java: porting to C++

分類Dev

How is an Or statement evaluated in VBA?

分類Dev

How is A *= B *= A *= B evaluated?

分類Dev

How to disable Ctrl+Shift+C in Firefox

分類Dev

Error using bit shift operator

分類Dev

How can I calculate powers of 2 using left shift operator for large powers?

分類Dev

How are non-string literals evaluated in Javascript?

分類Dev

How to tell if expression is evaluated at compile time or runtime?

分類Dev

How to get nil if something can't be evaluated?

分類Dev

OR operator in C

分類Dev

AND and OR operator c

分類Dev

Syntax errors when right shift operator is used as a template parameter

分類Dev

Top question`s answer gets shift operator wrong?

分類Dev

How to shift the reference cell in a formula?

分類Dev

How the unary minus operator works with integer literals in C++?

分類Dev

How to correctly do operator overloading in C++/CLI?

分類Dev

How to create a lazy-evaluated range from a file?

分類Dev

How `fix f = let {x = f x} in x` is evaluated?

分類Dev

How the value of type "i32" is evaluated in WebAssembly?

分類Dev

_add_ vs _radd_: how the expressions are evaluated?

分類Dev

When and how many times can a clojure file be evaluated with leiningen?

分類Dev

How to map “shift” key to “shift” + “capslock” using AutoHotkey?

分類Dev

C# Operator overloading ==

分類Dev

C++ - overloading [] operator

分類Dev

C++ "*" Operator Overloading

分類Dev

Struct in C with (.) or → operator

分類Dev

C ++ std :: function operator =

分類Dev

C++ Right Shift, XOR problems

分類Dev

How to shift several rows in a pandas DataFrame?

Related 関連記事

ホットタグ

アーカイブ