+ 1
Could someone explain the logic behind sum of x= 12|3 and y = 15|3 as 30?
I came across this problem in a Python quiz.
11 Respuestas
+ 4
In any programming language that supports the use of binary functions, the | operator is known as a bitwise or operator. What it does is it takes two decimal numbers and first checks both of their binary equivalents (ie. 5 = 101, 11 = 1011). It then compares the two, and for each 1 it encounters that is in either of the two numbers, it takes it and adds it to a new binary number. In other words, it takes whatever 1 it finds in the first decimal number or the second decimal number and adds it to a new set.
For example:
13|4
13 = 1101, 4 = 0100
13|4 = 1101 = 13
Here's a lesson on it that explains it better - https://www.sololearn.com/learn/4073/?ref=app
In the question you were given, it asks to return the sum of the calculations of 2 pairs of numbers using the bitwise or operator. The first of the 2 is 12|4, and the second is 15|3. Let's break down the calculations for each of the two expressions:
12|3
12 = 1100, 4 = 0011
12|4 = 1111 = 15
15|3
15 = 1111, 3 = 0011
15|3 = 1111 = 15
15 + 15 = 30
Hope this helped!
+ 4
@Faisal, I have doubts on this, so I tested 12|4 on RealCalc free and got 12 instead of 15, though if I pass 12|3 then I get 15 correctly. The 15|3 however matched (outputs 15), is the fault on the RealCalc app?
+ 4
@Ipang No RealCalc is not wrong, 12|4 is 1100 | 0100 which is 12
+ 4
@Louis, Thanks for confirmation : )
@Faisal, no problem mate, I guess you just made a typo there : )
+ 3
Well, as the bitwise or operator will always return a number, then really any operation can be used along with it. Furthermore, the number of digits in the binary of the two numbers doesn't really matter as long as you extend the length of the shorter one to match that of the longer one, which can be done by just adding a ton of zeroes at the end.
For example, say you had the expression 35|7. 35 can be expressed as 100011, and 7 can be expressed as 111. Just to make the comparison of the two binary numbers easier, it would be a good option to extend 111 to 000111 as it matches the length of 100011.
Also, sorry for screwing up the example in my explanation. d: I meant to use the same 2 expressions that you put in the description, but misread it as 12|4. If you want, I could change it to make it more accurate to your initial question?
+ 2
@Ipang Thanks, I just realised my mistake. I have no idea how I came up with 15 from that d:
+ 2
Got it. In the reply, Faisal has given an example of 12|4 and 15|3 and the sum is 27 while 12|3 and 15|3 is 30.
1 more question, Can we perform any arithmetic operations for any operator?
eg: 967|123 % 579|12?
or should both be of same no of digits?
34|5 % 54|4 (in this case 5,4 one digit)
Sorry for my technical English
+ 1
Thank you so much for the answer. Interest level has been raised exponentially only because of the quick Discussion section. Thanks Faisal and everyone. 😀
+ 1
@Faisal: Thanks. Understanding the concept was important and learned 1 more thing,need to be careful while reading the reply( you always check the problem first HaHa😂)
+ 1
haa, thanks for asking this question. because after you asked it i referenced the lesson on bitwise operators and now i fully understand them😁 also from the answers