maximizing-xor: c++ implementation
This commit is contained in:
parent
1bac478cb3
commit
eedc533521
2 changed files with 23 additions and 0 deletions
22
algorithms/bit-manipulation/maximizing-xor/maxXor.cpp
Normal file
22
algorithms/bit-manipulation/maximizing-xor/maxXor.cpp
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
uint32_t L, R, M;
|
||||||
|
cin >> L >> R;
|
||||||
|
M = L^R;
|
||||||
|
|
||||||
|
M--;
|
||||||
|
M |= M >> 1;
|
||||||
|
M |= M >> 2;
|
||||||
|
M |= M >> 4;
|
||||||
|
M |= M >> 8;
|
||||||
|
M |= M >> 16;
|
||||||
|
|
||||||
|
cout << M;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
@ -18,5 +18,6 @@ if __name__ == "__main__":
|
||||||
R = int(input())
|
R = int(input())
|
||||||
A, B, M = maxXor(L, R)
|
A, B, M = maxXor(L, R)
|
||||||
print("{}^{} = {}".format(A, B, M))
|
print("{}^{} = {}".format(A, B, M))
|
||||||
|
print("{}^{} = {}".format(bin(A), bin(B), bin(M)))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue