+ 1
check
I need to find the item whose binary record ends with no more than two zeros, is there another way to do this? except a[i]% 2 == 0 or ((a[i]%2==0) and (a [i] // 2)% 2 == 0 and ((a [i] // 2) // 2)% 2! = 0)
5 odpowiedzi
+ 1
your requirement description seems to mismatch your test^^
1) 0 should return True or False? If we consider 1 digit enough to binary representation there's no more than 2 0s, else if we consider infinite 0 left padding there's more than 2 0s) => my logic would said 2nd case (False), your test return True...
2) 1 should return True or False? My logic would say ends with no zeros, so there's no more than 2 0s (True) but your conditional return False....
Stick to my logic, you could check if a[i] & 7 (bitwise 'and' operator act as a mask, dec 7 is bin 111, if n & 7, bin(n) ends with no more than 2 0s (else ends with at least 3 0s).
Equivalent with % operator (less efficient) would be to test a[i] % 8:
Binary division by 2 is 1 digit shift to right, as decimal division by 10 is 1 digit shift to right:
dec 420 / dec 10 = dec 42
bin 110 / dec 2 = bin 11 (dec 6 / dec 2 = dec 3)
n % 8 is rest of n // 8 = rest of n // 2 // 2 // 2 (binary 3 digits shift to right) and should be != 0 else ends with at least 3 0s.
+ 1
1) that's exactly not what your code in the question description do ;P
2) it's enough to test for divisibility by 8 (either "a[i] != 0" OR "a[i] & 7" -- OR is NOT a logical operator here, obviously?) as if divisible by 8, necessarly divisible by 4 AND by 2 ;)
3) % is not a birtwise operator (it's a floating point operator) while & is (bitwise operate on bits. In this case you want to test the right most last 3 bits by use of one of these two way, but that's not meaning that bith operators are bitwise ^^
4) you doesn't answered my logic question about the expected result for special cases 0 and 1?...
0
I already figured out this task, it was necessary to use bitwise division. if (a[i]% 2 == 0 or a[i]% 4 == 0) and a[i]% 8! = 0:
0
an array with numbers was given. it was necessary to select from it those numbers whose binary notation ends in no more than two zeros. that is, the desired numbers either end with one zero or two zeros end in binary representation. you somehow understood the question in your own way.. either it's me too smart😅👽Listen to the song "Olesya" by VIA Pesnyary. lovely song.
0
I interpret the question in the way wich sounds the more logical for me, because there are many interpretation possible as you describe the requirement, as I explained in my first post... I could even try to reformulate it:
binary representation of a number could be done either by using less digit as possible (removing leading zeros), with fixed sized number of digits, or even considered as infinite left zeros padding....
Roughly:
42 in binary could be wrote as:
101010
or even as:
000...01010 (any numbers of zeros at left)
so, the 0 number/digit case could be considered either as ending "with no more than two zeros" (only one if we consider the less digit needed) or ending with an infinitie numbers of zeros (so much more than 2).
Similar for the 1 case... until you reformulate the requirement as "either end with one or two zeros" (wich is slightly different for your initial phrasing "with no more than two zeros" ^^