欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【hackerrank】-Day 29: Bitwise AND--not finished!

程序员文章站 2022-04-03 08:14:13
...

30-bitwise-and
Objective
Welcome to the last day! Today, we’re discussing bitwise operations. Check out the Tutorial tab for learning materials and an instructional video!

Task
Given set . Find two integers, and (where ), from set such that the value of is the maximum possible and also less than a given integer, . In this case, represents the bitwise AND operator.

Input Format

The first line contains an integer, , the number of test cases.
Each of the subsequent lines defines a test case as space-separated integers, and , respectively.

Constraints

Output Format

For each test case, print the maximum possible value of on a new line.

Sample Input

3
5 2
8 5
2 2
Sample Output

1
4
0
Explanation

All possible values of and are:

The maximum possible value of that is also is , so we print on a new line.


Current Buffer (saved locally, editable)

#!/bin/python3

import math
import os
import random
import re
import sys

def bitwiseand(n, k):
    if n <= k:
        print(0)
    else:
        s = {}
        for i in range(n):
            s[i]=i+1
        bitwise_and = []
        num = 1
        for i in range(n):
            if num >= n:
                break
            for j in s:
                bitwise_and.append(num & j)
            num += 1
            s.pop(i)
        bitwise_and_new = list(set(bitwise_and))
        for b in range(len(bitwise_and_new)):
            if bitwise_and_new[b] >= k:
                print(bitwise_and_new[b-1])
                break

if __name__ == '__main__':
    # the bad func ----Runtime Error :(
#     t = int(input())

#     for t_itr in range(t):
#         nk = input().split()
#         n = int(nk[0])
#         k = int(nk[1])
#         bitwiseand(n, k)

    # the best func, tell me why?
    T = int(input().strip())
    for _ in range(T):
        n , k = map(int , input().split())
        print(k-1 if ((k-1) | k) <= n else k-2)
相关标签: hackerrank