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

24 篇文章 0 订阅
24 篇文章 0 订阅

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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值