Skip to content

Instantly share code, notes, and snippets.

@ZenulAbidin
Created February 23, 2020 20:23
Show Gist options
  • Save ZenulAbidin/7a05c88b8cd6957a57b174bb9d0a579d to your computer and use it in GitHub Desktop.
Save ZenulAbidin/7a05c88b8cd6957a57b174bb9d0a579d to your computer and use it in GitHub Desktop.
Day 29: Bitwise AND
#!/bin/python3
import math
import os
import random
import re
import sys
if __name__ == '__main__':
t = int(input())
for t_itr in range(t):
nk = input().split()
n = int(nk[0])
k = int(nk[1])
maxab = 0
ab = (0, 0)
mk = k
mk |= mk >> 1
mk |= mk >> 2
mk |= mk >> 4
mk |= mk >> 8
mk |= mk >> 16
mk += 1
msbk = mk >> 1
if msbk == k:
msbk = int(msbk/2)
for b in range(1, min(2*k, n)+1):
for a in range(msbk, b):
aandb = a & b
if aandb > maxab and aandb < k:
ab = (a, b)
maxab = aandb
print(maxab)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment