Unexpected bit shift operation results ( a bug? )
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
Result of the bit shift operation like "(01011111 << 4) >> 4" should be "00001111" instead of "01011111".
The following codes showed an unexpected result while std::bitset works fine.
```c++
//main.cpp
#include
#include
#include
int main()
{
uint8_t i = 0x5F;
std::bitset<8> bs(i);
std::cout << "(0x5F >> 4) << 4 ==> " << std::bitset<8>((i >> 4) << 4) << std::endl;
std::cout << "(0x5F << 4) >> 4 ==> " << std::bitset<8>((i << 4) >> 4) << " (instead of 00001111 !!!)" << std::endl;
std::cout << "(bitset(01011111) << 4) >> 4 ==> " << ((bs << 4) >> 4) << std::endl;
std::cout << "uint8_t(0x5F << 4) >> 4 ==> " << std::bitset<8>(uint8_t(i << 4) >> 4) << std::endl;
}
```
Running results:
```
(0x5F >> 4) << 4 ==> 01010000
(0x5F << 4) >> 4 ==> 01011111 (instead of 00001111 !!!)
(bitset(01011111) << 4) >> 4 ==> 00001111
uint8_t(0x5F << 4) >> 4 ==> 00001111
```
compile command:
```console
clang++ main.cpp -o t
```
or
```console
g++ main.cpp -o t
```
Contributor guide
Research direction
Start by compiling and running the supplied main.cpp with both clang++ and g++, then inspect the differing expressions involving uint8_t and std::bitset. Check the language rules for integer promotion and shift operations before locating the relevant compiler tests. Done means establishing whether the output is conforming or, if not, providing a focused reproducer and regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100