Last active
August 8, 2017 11:53
-
-
Save mhmd-azeez/d2892ff1edaccac5f21aa7612bb59db9 to your computer and use it in GitHub Desktop.
Bitwise operators in C++
This file contains hidden or 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> | |
| #include <string> | |
| using namespace std; | |
| /* | |
| |-----|-----|-----|-----|-----|-----|-----|-----|-----| | |
| | n | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | | |
| |-----|-----|-----|-----|-----|-----|-----|-----|-----| | |
| |value| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | | |
| |-----|-----|-----|-----|-----|-----|-----|-----|-----| | |
| */ | |
| void main() | |
| { | |
| int num1 = 6; // 00000110 | |
| int num2 = 4; // 00000100 | |
| int result = num1 & num2; // 00000100 | |
| cout << bitset<8>(result) << " => " << result << endl; | |
| system("pause"); | |
| } |
This file contains hidden or 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> | |
| #include <string> | |
| using namespace std; | |
| /* | |
| |-----|-----|-----|-----|-----|-----|-----|-----|-----| | |
| | n | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | | |
| |-----|-----|-----|-----|-----|-----|-----|-----|-----| | |
| |value| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | | |
| |-----|-----|-----|-----|-----|-----|-----|-----|-----| | |
| */ | |
| void main() | |
| { | |
| int num1 = 6; // 00000110 | |
| int result = ~num1; // 11111001 | |
| // 00000111 = -7 | |
| cout << bitset<8>(result) << " => " << result << endl; | |
| system("pause"); | |
| } |
This file contains hidden or 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> | |
| #include <string> | |
| using namespace std; | |
| /* | |
| |-----|-----|-----|-----|-----|-----|-----|-----|-----| | |
| | n | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | | |
| |-----|-----|-----|-----|-----|-----|-----|-----|-----| | |
| |value| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | | |
| |-----|-----|-----|-----|-----|-----|-----|-----|-----| | |
| */ | |
| void main() | |
| { | |
| int num1 = 6; // 00000110 | |
| int num2 = 4; // 00000100 | |
| int result = num1 | num2; // 00000110 | |
| cout << bitset<8>(result) << " => " << result << endl; | |
| system("pause"); | |
| } |
This file contains hidden or 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> | |
| #include <string> | |
| using namespace std; | |
| /* | |
| |-----|-----|-----|-----|-----|-----|-----|-----|-----| | |
| | n | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | | |
| |-----|-----|-----|-----|-----|-----|-----|-----|-----| | |
| |value| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | | |
| |-----|-----|-----|-----|-----|-----|-----|-----|-----| | |
| */ | |
| void main() | |
| { | |
| int num1 = 6; // 00000110 | |
| int result = num1 << 2; // 00011000 | |
| cout << bitset<8>(result) << " => " << result << endl; | |
| system("pause"); | |
| } |
This file contains hidden or 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> | |
| #include <string> | |
| using namespace std; | |
| /* | |
| |-----|-----|-----|-----|-----|-----|-----|-----|-----| | |
| | n | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | | |
| |-----|-----|-----|-----|-----|-----|-----|-----|-----| | |
| |value| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | | |
| |-----|-----|-----|-----|-----|-----|-----|-----|-----| | |
| */ | |
| void main() | |
| { | |
| int num1 = 6; // 00000110 | |
| int result = num1 << 1; // 00000011 | |
| cout << bitset<8>(result) << " => " << result << endl; | |
| system("pause"); | |
| } |
This file contains hidden or 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> | |
| #include <string> | |
| using namespace std; | |
| /* | |
| |-----|-----|-----|-----|-----|-----|-----|-----|-----| | |
| | n | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | | |
| |-----|-----|-----|-----|-----|-----|-----|-----|-----| | |
| |value| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | | |
| |-----|-----|-----|-----|-----|-----|-----|-----|-----| | |
| */ | |
| void main() | |
| { | |
| int num1 = 6; // 00000110 | |
| int num2 = 4; // 00000100 | |
| int result = num1 ^ num2; // 00000010 | |
| cout << bitset<8>(result) << " => " << result << endl; | |
| system("pause"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks my techer