Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't left bit shift << shift beyond 31 for long int datatype?

Tags:

c++

bit-shift

I want to use the following code in my program but gcc won't allow me to left shift my 1 beyond 31.

sizeof(long int) displays 8, so doesn't that mean I can left shift till 63?

#include <iostream>

using namespace std;

int main(){
    long int x;
    x=(~0 & ~(1<<63));
    cout<<x<<endl;
    return 0;
}

The compiling outputs the following warning:

left shift `count >= width` of type [enabled by default] `x=(~0 & ~(1<<63))`;
                                                                    ^

and the output is -1. Had I left shifted 31 bits I get 2147483647 as expected of int.

I am expecting all bits except the MSB to be turned on thus displaying the maximum value the datatype can hold.

like image 536
Juan John Mathews Avatar asked Jun 09 '14 10:06

Juan John Mathews


People also ask

What happens when you shift bits to the left?

When shifting left, the most-significant bit is lost, and a 0 bit is inserted on the other end. The left shift operator is usually written as "<<".

How does a left shift work in bitwise?

Bitwise Shift Operators Left Shift(<<): The left shift operator, shifts all of the bits in value to the left a specified number of times. Syntax: value << num. Here num specifies the number of position to left-shift the value in value.

What does bit shifting by 1 do?

Bitshifting shifts the binary representation of each pixel to the left or to the right by a pre-defined number of positions. Shifting a binary number by one bit is equivalent to multiplying (when shifting to the left) or dividing (when shifting to the right) the number by 2.

What are bit shifts used for?

A bit shift is a bitwise operation where the order of several bits is moved, either to the left or right, to efficiently perform a mathematical operation. Bit shifts help with optimization in low-level programming because they require fewer calculations for the CPU than conventional math.


2 Answers

Although your x is of type long int, the 1 is not. 1 is an int, so 1<<63 is indeed undefined.

Try (static_cast<long int>(1) << 63), or 1L << 63 as suggested by Wojtek.

like image 197
Luchian Grigore Avatar answered Oct 19 '22 05:10

Luchian Grigore


You can't use 1 (int by default) to shift it beyond the int boundaries.

There's an easier way to get the "all bits except the MSB turned on" for a specific datatype

#include <iostream>
#include <limits>

using namespace std;

int main(){
    unsigned long int max = std::numeric_limits<unsigned long int>::max();
    unsigned long int max_without_MSB = max >> 1;
    cout<< max_without_MSB <<endl;
    return 0;
}

note the unsigned type. Without numeric_limits:

#include <iostream>
using namespace std;

int main() {

    long int max = -1;
    unsigned long int max_without_MSB = ((unsigned long int)max) >> 1;
    cout << max_without_MSB << endl;

    return 0;
}
like image 38
Marco A. Avatar answered Oct 19 '22 04:10

Marco A.