LoginSignup
0
0

More than 5 years have passed since last update.

Shift Operations 🧸

Last updated at Posted at 2019-04-22

a << i is the same as a * pow(2, i)

<<
#include <iostream> 
#include <math.h>
using namespace std;

int main(void) {
    unsigned int a = 5, b = 48;
    for (int i = 0; i < 10; i++) {
        cout << (a << i) << " " << a * pow(2, i) << endl; 
    }
    return 0;
}

/*
5 5
10 10
20 20
40 40
80 80
160 160
320 320
640 640
1280 1280
2560 2560
*/

a >> i is the same as a / (int)pow(2, i)

>>
#include <iostream> 
#include <math.h>
using namespace std;

int main(void) {
    unsigned int a = 48;
    for (int i = 0; i < 10; i++) {
        cout << (a >> i) << " " << a / (int)pow(2, i) << endl; 
    }
    return 0;
}

/*
48 48
24 24
12 12
6 6
3 3
1 1
0 0
0 0
0 0
0 0
*/
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0