Skip to content

Instantly share code, notes, and snippets.

@mhmd-azeez
Last active August 8, 2017 11:53
Show Gist options
  • Select an option

  • Save mhmd-azeez/d2892ff1edaccac5f21aa7612bb59db9 to your computer and use it in GitHub Desktop.

Select an option

Save mhmd-azeez/d2892ff1edaccac5f21aa7612bb59db9 to your computer and use it in GitHub Desktop.
Bitwise operators in C++
#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");
}
#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");
}
#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");
}
#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");
}
#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");
}
#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");
}
@hawkar2

hawkar2 commented Aug 8, 2017

Copy link
Copy Markdown

thanks my techer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment