Monday, 27 April 2020

[Bitwise OR shenanigans] ACM ICPC Team

This question is really easy, but a little hitch stopped me on this one:

Bitwise OR in common programming languages:
| operator accepts two integers, converts them to binary, ORs them, and returns the result in decimal again.
If in a | b, a and b are given in binary, we need to add a "0b" before them. This is what I did not know.

I use Python3 for competitive coding, so for reference I shall add code to compare two integers which consist only of zeroes and ones

orint(a, 2) | int(a, 2)
orbin(or)[2:]

the first line converts a and b to integer and the second parameter "2" tells python that a is a string of zeroes and ones which is a binary number.
Hence a = "1001" yields 9.

or is set to a | b in DECIMAL system. The second line converts it to binary.
The [2:] tells python to skip the "0b". <== this is what isn't intuitive, and frankly, kind of sucks. But OK. One more thing to keep in mind!

No comments:

Post a Comment