-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunsc5.cpp
More file actions
32 lines (28 loc) · 842 Bytes
/
funsc5.cpp
File metadata and controls
32 lines (28 loc) · 842 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <bitset>
#include <string>
//ñàìà ôóíêöèÿ ñìåíû áèòîâ
void swapBits(unsigned long long int& n, int p1, int p2) {
int bit1 = (n >> p1) & 1;//ïîëó÷åíèå çíà÷åíèÿ áèòà
int bit2 = (n >> p2) & 1;
if (bit1 != bit2) {
n ^= (1 << p1) | (1 << p2);
//åñëè çíà÷åíèÿ áèòîâ íå îäèíàêîâûå, òî ìåíÿåì èõ ìåñòàìè («^» îïåðàöèÿ ïîáèòîâîãî èñêëþ÷àþùåãî èëè)
}
}
<<<<<<< HEAD
=======
>>>>>>> 07f38bcb5a8691e0f9da8b0e3934363f1fd0a694
//ïðîñòî äëÿ êðàñèâîãî âûâîäà
void printBinary(unsigned long long int& n) {
std::bitset<64> binary(n);
std::string binary_str = binary.to_string();
int pos = binary_str.find_first_not_of('0');
if (pos != -1) {
binary_str = binary_str.substr(pos);
}
else {
binary_str = "0";
}
std::cout << binary_str << std::endl;
}