Something I discovered doing logic

  • Context: High School 
  • Thread starter Thread starter vin300
  • Start date Start date
  • Tags Tags
    Logic
Click For Summary
SUMMARY

The discussion reveals that the sum of the results from the bitwise AND operation (P & Q) and the bitwise OR operation (P | Q) on two numbers is equal to the arithmetic sum of those two numbers (P + Q). This is confirmed through an example using P = 5 and Q = 6, demonstrating that (P & Q) + (P | Q) equals P + Q. The operations utilize binary representations, where P & Q identifies common bits set to 1, while P | Q identifies all bits set to 1 in either number.

PREREQUISITES
  • Understanding of binary number representation
  • Familiarity with bitwise operators: AND (&) and OR (|)
  • Basic knowledge of arithmetic operations
  • Experience with programming concepts related to binary arithmetic
NEXT STEPS
  • Study binary arithmetic and its applications in programming
  • Learn about bitwise operations in various programming languages, such as Python and C++
  • Explore the implications of bitwise operations on performance in algorithms
  • Investigate how bitwise operations are utilized in data structures like binary trees
USEFUL FOR

Mathematicians, computer scientists, software developers, and anyone interested in understanding binary operations and their applications in programming and algorithms.

vin300
Messages
602
Reaction score
4
I haven't known about a law that says so. Step 1: using two numbers in any format: binary, octa, hexa, do an AND operation. Step 2: Perform OR operation on the same numbers. Step 3: Add the results of step 1 and 2 numerically. The result comes out to be the same as the sum of original numbers.
 
Physics news on Phys.org
Give an example to explain what you mean by performing an "AND" and an "OR" operation on two numbers. Show an example in ordinary base 10 representation.
 
Hmm I wonder if "X and Y" simply means min(X,Y) and "X or Y" means max(X,Y), when applied to numbers. If so, what vin300 has discovered is just that min(X,Y)+max(X,Y)=X+Y, which is easy to see.
 
vin300 said:
I haven't known about a law that says so. Step 1: using two numbers in any format: binary, octa, hexa, do an AND operation. Step 2: Perform OR operation on the same numbers. Step 3: Add the results of step 1 and 2 numerically. The result comes out to be the same as the sum of original numbers.

What you "discovered" is that P&Q added to P|Q is equal to P+Q, right?

Here's a short explanation of what's really happening. Using the bitwise operator "&" for the logical operator "AND" and the bitwise operator "|" for the logical operator "OR", evaluating the expression P&Q basically creates a binary number with "1" at each position where both numbers have a "1" and puts zeros elsewhere; the expression P|Q does the same thing but puts a "1" at each position where at least one of the two numbers has a "1" and also puts zeros elsewhere. So when you add both expressions P|Q and P&Q, you're basically adding P and Q and the remainders, which gives you P+Q. This works under binary arithmetic since the only possible operations we have for addition are:
  • 0 + 0 = 0
  • 0 + 1 = 1
  • 1 + 0 = 1
  • 1 + 1 = 0, and carry 1 to the next more significant bit, i.e. in this direction ←
In other words, when you evaluate P&Q or P|Q, every position where P and Q both had zeros will remain zero; likewise, every position where at least P or Q or both had "1" will remain the same. Since in the last case one event is included in the other, this assures you under the arithmetic rules we agreed on that adding P&Q and P|Q is equivalent to add P and Q.

Here is an example with P=5 and Q=6:
Code:
Expression     Result     Binary Description
P              5          00000101
Q              6          00000110
P & Q          4          00000101 & 00000110 = 00000100
P | Q          7          00000101 | 00000110 = 00000111

When you add P&Q and P|Q, you have:
Code:
      1
  00000100 = P & Q
+ 00000111 = P | Q
----------
  00001011 = (P & Q) + (P | Q)
And when you add P and Q, you have:
Code:
      1
  00000101 = P
+ 00000110 = Q
----------
  00001011 = P + Q
We can see that both expressions are equal. Of course the mathematical representation of bitwise operations can get messier depending of what we're using, e.g. if P and Q are not positive and/or integers, but that's the general procedure used to handle these operations, and basically every programming language that I know of does that.
 
  • Like
Likes   Reactions: micromass and berkeman

Similar threads

  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 18 ·
Replies
18
Views
5K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
2
Views
2K
  • · Replies 45 ·
2
Replies
45
Views
6K
  • · Replies 15 ·
Replies
15
Views
3K