Bit shift compiler error or angle register?

The following code outputs are 0,1,32,33. Which, on the contrary, is intuitive. But if I replaced the literal 1 with the type of the annotated constant "ONE", the loop works fine.

This is with gcc 4.6.2 and -std = C ++ 0x.

#include<iostream>
#include<cstdint>
using namespace std;
int main()
    {
    int64_t bitmask = 3;
    int64_t k;
    const int64_t ONE = 1;
    cout<<"bitmask = "<<bitmask<<endl;

    for(k=0; k<64; k++)
        {
        if(bitmask & (1<<k))
            {
            cout<<"k="<<k<<endl;
            }
        }

    return 0;
    } 

EDIT  Question: As Ben noted, 1 is considered to be 32-bit by default. Why it does not advance to 64 bits when its shared operand is 64 bits.

Decision

No. & L; <does not require each side to be of the same type. After all, why do the right side of int64_t when the maximum shift is available in char? Advancement occurs only when you are dealing with arithmetic operators, and not with all operators.

Copied from Bill's comments below.

+5
1

: (1<<k).

1 , int.

int 64 , (1<<k) undefined , k . - Intel, undefined , Intel - .

, (1LL<<k)


( 5.8 expr.shift):

, . - . undefined, .

" ", , , .

++ 03 ++ 11.

+7

All Articles