Is not(A XOR B) the same as (!A XOR !B)? - xor

I would like to know if !(A xor B) is equal to (!A xor !B)?
I am struggling to understand the logic behind this problem.

They are not equal. You could check the following table for further explanation.
+---+---+-------+--------+----+----+-------+
| A | B | (A^B) | !(A^B) | !A | !B | !A^!B |
+---+---+-------+--------+----+----+-------+
| 0 | 0 | 0 | 1 | 1 | 1 | 0 |
| 0 | 1 | 1 | 0 | 1 | 0 | 1 |
| 1 | 0 | 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 0 | 1 | 0 | 0 | 0 |
+---+---+-------+--------+----+----+-------+
Edit: Computing !(A^B) without using NOT operation with A, B, A' and B'
XOR(A, B) = OR(AND(A, B'), AND(A', B))
After using DeMorgan for the equation above:
NOT XOR(A,B) = AND(OR(A', B), OR(A, B'))

If in doubt, use truth tables. A and B can be 1 or 0 so:
A xor B:
0 1
1 0
! (A xor B)
1 0
0 1
! A xor ! B:
0 1
1 0
So, the answer is no. They seem to be the same as the initial xor.

Going step by step, and looking at the resulting column, we see that they do not result in the same output based on the same input.
A
B
A XOR B
not(A XOR B)
0
0
0
1
0
1
1
0
1
0
1
0
1
1
0
1
A
B
!A
!B
(!A XOR !B)
0
0
1
1
0
0
1
1
0
1
1
0
0
1
1
1
1
0
0
0

No they're not.
A xor B is equal to 1 if and only if either A or either B is 1 but not both. Therefore !(A xor B) is equal to 1 if and only if both A and B are equal.
Whereas with (!A xor !B) you first flip the bits and then do the XOR. So (!A xor !B) = (A xor B).
Here is the truth table for the first one:
A | B | A xor B | !(A xor B)
============================
0 | 0 | 0 | 1
0 | 1 | 1 | 0
1 | 0 | 1 | 0
1 | 1 | 0 | 1
and for the second one:
A | B | !A | !B | (!A xor !B)
=============================
0 | 0 | 1 | 1 | 0
0 | 1 | 1 | 0 | 1
1 | 0 | 0 | 1 | 1
1 | 1 | 0 | 0 | 0

Related

How to create array of given length and same number?

When using SUMSERIES I need to specify "the array or range containing the coefficients of the power series" but I want to make it so the number of elements is dynamic while the element itself (1) remains the same.
Example:
SUM FROM 0 TO N of x^1,5
(cell) Length of series N : 7 -- > SUMSERIES(1,5;0;1;{1,1,1,1,1,1,1})
But I should be able to change the seven for a 3 and get --> SUMSERIES(1,5;0;1;{1,1,1})
In Java for example you'd declare and instantiate the array --> int[] arr = new int[N];
And then fill in a loop --> for(int i = 0; i <arr.length; i++) {arr[i] = 1,5}
Thanks in advance and sorry if the explanation isn´t clear, it's my first time around hehe
this should work:
=SUMSERIES(1,5;0;1;SEQUENCE(1,[cell],1,0))
try:
=ARRAYFORMULA(SIGN(TRANSPOSE(ROW(INDIRECT("A1:A"&A1)))))
and then:
=INDEX(SUMSERIES(1,5; 0; 1; SIGN(TRANSPOSE(ROW(INDIRECT("A1:A"&A1))))))
In older version of Excel you can get this array using this (all of them are array formulas)
=INDEX(MUNIT(n),1,0)*0+x for horizontal array
=INDEX(MUNIT(n),0,1)*0+x for vertical array
Where:
n is dimension of the array
x is value of each item in the array
How it works:
MUNIT creates an identity matrix of size N
+---++---+---+---+---+---+
| || 1 | 2 | . | . | n |
+---++---+---+---+---+---+
+---++---+---+---+---+---+
| 1 || 1 | 0 | 0 | 0 | 0 |
| 2 || 0 | 1 | 0 | 0 | 0 |
| . || 0 | 0 | 1 | 0 | 0 |
| . || 0 | 0 | 0 | 1 | 0 |
| n || 0 | 0 | 0 | 0 | 1 |
+---++---+---+---+---+---+
Now we extract one (the first) row/column (n is set to 7 here)
=INDEX(MUNIT(7),1,0) for row extraction
=INDEX(MUNIT(7),0,1) for column extraction
And fill it with desired number (desired number here is 9 here)
=INDEX(MUNIT(7),1,0)*0+9 for row
=INDEX(MUNIT(7),0,1)*0+9 for column

How to print numbers from 1 to 10 using a loop in Brainfuck? Is it even possible?

How to print numbers from 1 to 10 using a loop in Brainfuck? Is it even possible?
I am looking for a solution to this issue.
+++++++++++++++++++++++++++++++++++++++++++++++++ Cell 0 to '1'
>++++++++++ cell 1 to '\n'
>+++++++++ cell 2 to 9 as counter
[ Print numbers 1 to 9
<< Data pointer to cell 0
.+ Print and increment cell 0
>. Data pointer to cell 1 and print the newline
>- Data pointer to cell 2 and decrement counter
] Loop till counter is 0
+++++++++ Set cell 2 to 9
[ Set cell 0 to '1'
<<- Data pointer to cell 0 and decrement
>>- Data pointer to cell 2 and decrement counter
] Loop till counter is 0
<<. Data pointer to cell 0 and print '1'
-. Decrement cell 0 and print '0'
>. Data pointer to cell 1 and print newline
Readable version:
+++++++++++++++++++++++++++++++++++++++++++++++++>
++++++++++>
+++++++++[<<.+>.>-]
+++++++++[<<->>-]
<<.-.>.
Output:
1
2
3
4
5
6
7
8
9
10
Live demo:
Brainf**k print 1 to 10
Brainf**k Visualizer
TL;DR
-[>+<-----]>---<++++++++++<++++++++++[>>.+<.<-]>>---------.-.
Try it online!
END TL;DR
In order to program in BrainF**k, pretend like every program (even simple ones) will need to start out with a layout.
The pseudo-code for this would be something like:
Generate the character '0'
Move left and generate '\n'
Move left and generate the counter (10 numbers in this case)
Loop: Get back to the character '0', print it, increment it to '1', go to the newline, print it, go to the counter, and decrement it. End it when the counter is 0
Generate '1' and print it
Generate '0' and print it
However, the last two steps could be simplified to just:
Go back to the digit '9'
Decrement it until '1' and print
Decrement it until '0' and print
This saves a lot of time and bytes characters.
To generate the character '0', you generate the integer 48 (because that's it's ASCII value). To do this you can go to Esolangs' BF Constants. Looking up the number 48, we find -[>+<-----]>---
Our program so far is -[>+<-----]>--- to generate 0
Next, move left and generate \n (newline). We can use <++++++++++. Notice how it is completely plus signs. This is because there is not much room to reduce the character count at the number 10.
Our program so far is -[>+<-----]>---<++++++++++
Then, move left and generate the counter. We want the counter to be 10 to print numbers from 0 to 9. <++++++++++.
Our program so far is -[>+<-----]>---<++++++++++<++++++++++
After that, start the loop [. Go to the '0' >>, print it ., increment it +, go to the newline and print <., Go to the counter and decrement it, and end the loop when it is zero <-]. [>>.+<.<-]
Our program so far is -[>+<-----]>---<++++++++++<++++++++++[>>.+<.<-]
Finally, go to the '9' >>, decrement it until it is 1 and print ---------., and decrement it until it is 0 and print -.. ---------.-.
The program is finished.
++++++++++++++++++++++++++++++++++++++++++++++++ Let address 0 be the digit we want to print, starting with '0'
>++++++++++ Let address 1 be our newline character
>+++++++++ Let address 2 be our counter, starting at 9
[
- Decrement the counter
<<+. Increment the digit we want to print and print it
>. Print the newline
> Make sure we're at the counter again before we loop back
]
<< Move back to address 0
--------. Make address 0 '1'
-. Make address 0 '0'
Live demo
This is possible. Here's the code: ++++++++++>++++++++++[>+++++<-]>-.<<.>>+.<<.>>+.<<.>>+.<<.>>+.<<.>>+.<<.>>+.<<.>>+.<<.>>+.<<.>>--------.-. It could be shorter, however, it still accomplishes the same task. Brainf*** can theoretically perform any computation, as it is Turing complete. This is just one of those computations.
I suggest the following:
+++++++++>>++++++++[-<++++++>][-]>++++[-<++++++++>]<<<[->+.>.<<]>[-]+++++++[-<+++++++>]<.-.
It outputs 1 2 3 4 5 6 7 8 9 10.
We have this (each line is after the next cell-affecting instruction, which are + - .):
| C1 | C2 | C3 | C4 | Output |
+----+----+----+----+-----------------------+
| 0 | 0 | 0 | 0 | |
| 1 | 0 | 0 | 0 | |
| 2 | 0 | 0 | 0 | |
...
| 8 | 0 | 0 | 0 | |
| 9 | 0 | 0 | 0 | |
... (first loop)
| 9 | 48 | 0 | 0 | |
| 9 | 48 | 0 | 1 | |
| 9 | 48 | 0 | 2 | |
| 9 | 48 | 0 | 3 | |
| 9 | 48 | 0 | 4 | |
| 9 | 48 | 0 | 3 | | (enter second loop)
| 9 | 48 | 1 | 3 | |
| 9 | 48 | 2 | 3 | |
...
| 9 | 48 | 8 | 3 | |
| 9 | 48 | 8 | 2 | |
| 9 | 48 | 9 | 2 | |
...
| 9 | 48 | 16 | 2 | |
| 9 | 48 | 16 | 1 | |
| 9 | 48 | 17 | 1 | |
...
| 9 | 48 | 24 | 1 | |
| 9 | 48 | 24 | 0 | |
| 9 | 48 | 25 | 0 | |
...
| 0 | 48 | 32 | 0 | | (exit second loop)
| 8 | 48 | 32 | 0 | | (enter third loop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| 57 | 32 | 0 | 1_2_3_4_5_6_7_8_9_ | (exit third loop)
+----+----+----+----+-----------------------+
Then cell C2 is set to 0 and used to set cell C1 to 49 (charater 1).
It prints C1, then removes one and prints it again, giving an output of:
1_2_3_4_5_6_7_8_9_10 (underscores are spaces)
It's for me the simplest program to print the integers from 1 to 10.
If you want each number to be on its own line, replace the >>>++++[-<++++++++>]<<< by ++++++++++.
Issue [SOLVED]: instead of printing 10 at the end, it prints :...

C if(var & something) [duplicate]

I am lacking some basic understanding in bitwise '&' operator.
5 = 101
4 = 100
So why the output of the below if condition is true cause and of bits 101 & 100 should be false:
#include <stdio.h>
main()
{
if(5&4)
printf("Yes\n");
}
5 is 101.
4 is 100.
5 & 4 is not 0:
101
100 &
↓↓↓
100
Problem solved ✓
Clarification:
In C, every non-zero value satisfies the if condition. Meaning, if you write:
if (-5) {
if (100) {
// reachable code
}
}
Whereas:
if (0) {
destroyTheWorld(); // we are safe
}
5 - 101
4 - 100
5&4 - 100
It is true.
Understanding bitwise operator truth tables is crucial. Consider the following, where A and B are inputs and Y is the output.
& (Bitwise And) When inputs A and B are true, output is true; otherwise output is false
A B Y
---------
0 | 0 | 0
0 | 1 | 0
1 | 0 | 0
1 | 1 | 1
| (Bitwise Or) When A or B or both inputs are true output is true; otherwise output is false
A B Y
---------
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 1
^ (Bitwise X-Or) When A and B are opposite states, output is true; otherwise output is false
A B Y
---------
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 0
! (Bitwise Not) Output is the opposite state of the input
A Y
-----
0 | 1
1 | 0
Your Equation (5 & 4) == (0101 & 0100) == 0100 == 4 == true
0101
& 0100
------
0100
Because 0b100 & 0b101 equals 0b100 and the latter does not equal 0.
0b101 & 0b100 = 0b100
or
5&4 = 4
and 4 is non-zero and prints Yes
It enters the if condition. Because after the & operation it returns non-zero value. In C, for all non-zero value it's like returning true.

use ((c1^c2) & ~32) to test if c1 and c2 are the same character in different case

I saw some code like this
if( ((c1^c2) & ~32)==0 )
{
...
}
In this snippet the code likely mean that if the if statement is true, then c1 and c2 are the same character in different case, meaning that one of those is +32 or -32 away from the other. Why is that?
I did test myself and discover that in some case it is true while in others not:
printf("%d", (65^97)& ~32); //output is 0. right
printf("%d", (97^65)& ~32); //output is 0. right
printf("%d", (50^82)& ~32); //output is 64!! not the same though 82-50=32
Why is that? what is the magic in it?
(c1^c2) & ~32) xors c1 and c2, the result contains the bits that are in both characters and & with ~32 clears (ignores) the bit 5. (It is zeroed whether it was same in both or not). Comparing this with zero, checks if all the bits other than bit 5 are same.
This can be used to check if 2 letters are equal ignoring their case in ascii representation if you are sure that atleast c1 or c2 is a valid latin character(a-z, A-Z).
To understand this, let's pick 2 characters with different case and compare them:
+---+---+---+---+---+---+---+---+
a | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 1 |
+---+---+---+---+---+---+---+---+
| x | | | | | |
+---+---+---+---+---+---+---+---+
A | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 |
+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+
a ^ A | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+
32 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+
~32 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+
& | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+
You can try the same with j v/s J or t v/s z.
So there is no magic involved, only this logic.
Sometimes this condition is also written as:
if (ch1 == ch2 || (ch1 ^ 32) == ch2)
{
...
}

Bitwise '&' operator

I am lacking some basic understanding in bitwise '&' operator.
5 = 101
4 = 100
So why the output of the below if condition is true cause and of bits 101 & 100 should be false:
#include <stdio.h>
main()
{
if(5&4)
printf("Yes\n");
}
5 is 101.
4 is 100.
5 & 4 is not 0:
101
100 &
↓↓↓
100
Problem solved ✓
Clarification:
In C, every non-zero value satisfies the if condition. Meaning, if you write:
if (-5) {
if (100) {
// reachable code
}
}
Whereas:
if (0) {
destroyTheWorld(); // we are safe
}
5 - 101
4 - 100
5&4 - 100
It is true.
Understanding bitwise operator truth tables is crucial. Consider the following, where A and B are inputs and Y is the output.
& (Bitwise And) When inputs A and B are true, output is true; otherwise output is false
A B Y
---------
0 | 0 | 0
0 | 1 | 0
1 | 0 | 0
1 | 1 | 1
| (Bitwise Or) When A or B or both inputs are true output is true; otherwise output is false
A B Y
---------
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 1
^ (Bitwise X-Or) When A and B are opposite states, output is true; otherwise output is false
A B Y
---------
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 0
! (Bitwise Not) Output is the opposite state of the input
A Y
-----
0 | 1
1 | 0
Your Equation (5 & 4) == (0101 & 0100) == 0100 == 4 == true
0101
& 0100
------
0100
Because 0b100 & 0b101 equals 0b100 and the latter does not equal 0.
0b101 & 0b100 = 0b100
or
5&4 = 4
and 4 is non-zero and prints Yes
It enters the if condition. Because after the & operation it returns non-zero value. In C, for all non-zero value it's like returning true.

Resources