Created
September 15, 2018 03:28
-
-
Save completejavascript/0a58220eddac44d2bf362a78077d8e4b 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> | |
using namespace std; | |
int main() | |
{ | |
// Comment dòng này trước khi submit | |
freopen("input.txt","r",stdin); | |
// Nhập đầu vào | |
int L,R; | |
cin >> L >> R; | |
// Một số nguyên thì tối đa là 32 bít | |
// được đánh dấu từ vị trí 0 đến vị trí 31 | |
// Do đó khởi tạo pos = 31, tức là trỏ đến vị trí bit cao nhất | |
int pos = 31; | |
while(true) | |
{ | |
// Kiểm tra tại vị trí đó | |
// Và dịch dần cho đến khi tìm được vị trí thỏa mãn | |
// 1 số được biểu diễn bởi bit 1 và số còn lại được | |
// biểu diễn bởi bit 0 | |
bool check_right = R & (1 << pos); | |
bool check_left = L & (1 << pos); | |
// Nếu tìm được thì break khỏi vòng lặp | |
// Ngược lại, thì dịch | |
if(check_right && !check_left) break; | |
else pos--; | |
} | |
// Kết quả tìm được sẽ là số dạng nhị phân: 0b1111....111 | |
int result = 0; | |
for(int i = 0; i <= pos; i++) | |
result = result * 2 + 1; | |
cout << result << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment