-
-
Save serboox/e584aac6630f3065796702cfcd1efe18 to your computer and use it in GitHub Desktop.
Сдвиги на примере кода С++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <bitset> | |
using namespace std; | |
int main() | |
{ | |
/* 1) Если мы делаем сдвиг влево для положительного числа, мы можем получить отрицательное число | |
32:00000000000000000000000000000001->1 | |
31:10000000000000000000000000000000->-2147483648 | |
30:01000000000000000000000000000000->1073741824 | |
0:00000000000000000000000000000001->1 */ | |
int a = -1; | |
for (int i = 32; i >=0; i--) { | |
cout << i << ":" << bitset<32>(a<<i) << "->" << (a<<i) << "\n" << endl; | |
} | |
/* 2) Если мы делаем сдвиг вправо для отрицательного числа, мы не получаем положительного числа | |
32:11111111111111111111111111111111->-1 | |
31:10000000000000000000000000000000->-2147483648 | |
30:11000000000000000000000000000000->-1073741824 | |
0:11111111111111111111111111111111->-1 */ | |
a = 1; | |
for (int i = 32; i >=0; i--) { | |
cout << i << ":" << bitset<32>(a<<i) << "->" << (a<<i) << "\n" << endl; | |
} | |
/* 3) Если двигать вправо отрицательное число, оно будет заполнятся единицами | |
10000000000000000000000000000000->-2147483648 | |
11111111111000000000000000000000->-2097152 */ | |
a = -2147483648; | |
cout << bitset<32>(a>>10) << "->" << (a>>10) << "\n" << endl; | |
/*4) Если двигать вправо положительное число, оно будет заполнятся нулями | |
00000000000000000000000000000001->1 | |
00000000000000000000000000000000->0*/ | |
a = 1; | |
cout << bitset<32>(a>>10) << "->" << (a>>10) << "\n" << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment