>> - right shift

suggest change
#include <iostream>

int main(int argc, char **argv) {
    int a = 2;      // 0010b
    int b = a >> 1; // 0001b
    
    std::cout << "a = " << a << ", b = " << b << std::endl;
}
a = 2, b = 1

Why

The right bit wise shift will shift the bits of the left hand value (a) the number specified on the right (1); it should be noted that while the operation of a right shift is standard, what happens to the bits of a right shift on a signed negative number is implementation defined and thus cannot be guaranteed to be portable, example:

int a = -2;    
int b = a >> 1; // the value of b will be depend on the compiler

It is also undefined if the number of bits you wish to shift by is a negative number, example:

int a = 1;
int b = a >> -1;  // undefined behavior

The bit wise right shift does not change the value of the original values unless specifically assigned to using the bit wise assignment compound operator >>=:

#include <iostream>

int main(int argc, char **argv) {
    int a = 2;  // 0010b
    a >>= 1;    // a = a >> 1;

    std::cout << "a = " << a  << std::endl;
}
a = 1

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents