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

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)
{
...
}

Related

counting new lines, new tabs and spaces in C using getchar()

I created a table that count new line, new words, sapces and tabs.
I successfully count all of them but the problem is on the output:
I am trying to print one line only with the result with what I am am counting but instead the output is the whole count (the increment).
#include <stdio.h>
#define IN 1
#define OUT 0
int main()
{
int c,nl,nw,nc,state;
int newspace, tabchar;
state = OUT;
int id;
nl = nw = nc = id = 0;
newspace = tabchar = 0;
printf("+===============================================================+\n");
printf("| WORDS, NEW LINE COUNTER |\n");
printf("+======+==========+==========+============+===========+=========+\n");
printf("| ID | NEW LINE | NEW WORD | CHAR COUNT | NEW SPACE | NEW TAB |\n");
printf("+===============================================================+\n");
while ((c = getchar()) != EOF)
{
++nc;
if(c == '\n')
++nl;
if(c == ' ' || c == '\n' || c == '\t')
state = OUT;
if(c == ' ')
newspace++;
if(c == '\t')
tabchar++;
else if(state == OUT)
{
state = IN;
++nw;
}
id = nl + 1;
printf("| %d | %d | %d | %d | %d | %d |\n", id, nl, nw, nc, newspace, tabchar);
}
}
WRONG OUTPUT:
+===============================================================+
| WORDS, NEW LINE COUNTER |
+======+==========+==========+============+===========+=========+
| ID | NEW LINE | NEW WORD | CHAR COUNT | NEW SPACE | NEW TAB |
+===============================================================+
I am counting spaces and tab here
| 1 | 0 | 1 | 1 | 0 | 0 |
| 1 | 0 | 2 | 2 | 1 | 0 |
| 1 | 0 | 2 | 3 | 1 | 0 |
| 1 | 0 | 2 | 4 | 1 | 0 |
| 1 | 0 | 3 | 5 | 2 | 0 |
| 1 | 0 | 3 | 6 | 2 | 0 |
| 1 | 0 | 3 | 7 | 2 | 0 |
| 1 | 0 | 3 | 8 | 2 | 0 |
| 1 | 0 | 3 | 9 | 2 | 0 |
| 1 | 0 | 3 | 10 | 2 | 0 |
| 1 | 0 | 3 | 11 | 2 | 0 |
| 1 | 0 | 3 | 12 | 2 | 0 |
| 1 | 0 | 3 | 13 | 2 | 0 |
| 1 | 0 | 4 | 14 | 3 | 0 |
| 1 | 0 | 4 | 15 | 3 | 0 |
| 1 | 0 | 4 | 16 | 3 | 0 |
| 1 | 0 | 4 | 17 | 3 | 0 |
| 1 | 0 | 4 | 18 | 3 | 0 |
| 1 | 0 | 4 | 19 | 3 | 0 |
| 1 | 0 | 4 | 20 | 3 | 0 |
| 1 | 0 | 5 | 21 | 4 | 0 |
| 1 | 0 | 5 | 22 | 4 | 0 |
| 1 | 0 | 5 | 23 | 4 | 0 |
| 1 | 0 | 5 | 24 | 4 | 0 |
| 1 | 0 | 6 | 25 | 5 | 0 |
| 1 | 0 | 6 | 26 | 5 | 0 |
| 1 | 0 | 6 | 27 | 5 | 0 |
| 1 | 0 | 6 | 28 | 5 | 0 |
| 1 | 0 | 7 | 29 | 6 | 0 |
| 1 | 0 | 7 | 30 | 6 | 0 |
| 1 | 0 | 7 | 31 | 6 | 0 |
| 1 | 0 | 7 | 32 | 6 | 0 |
| 1 | 0 | 7 | 33 | 6 | 0 |
| 2 | 1 | 8 | 34 | 6 | 0 |
DESIRED OUTPUT:
+===============================================================+
| WORDS, NEW LINE COUNTER |
+======+==========+==========+============+===========+=========+
| ID | NEW LINE | NEW WORD | CHAR COUNT | NEW SPACE | NEW TAB |
+===============================================================+
I am counting spaces and tab here
| 2 | 1 | 8 | 34 | 6 | 0 |
What am I doing wrong? The output seems almost correct. It seems to me that the potential problem could be that the program does not understand properly the EOF in particular the following statement while ((c = getchar()) != EOF) but I am not sure why.
Thanks for pointing in the correct route for the solution.
It's actually working well but you're printing on every iteration.
For your desired output, you should put the printf statement after the while block ends.
while ((c = getchar()) != EOF)
{
++nc;
if(c == '\n')
++nl;
if(c == ' ' || c == '\n' || c == '\t')
state = OUT;
if(c == ' ')
newspace++;
if(c == '\t')
tabchar++;
else if(state == OUT)
{
state = IN;
++nw;
}
id = nl + 1;
printf("| %d | %d | %d | %d | %d | %d |\n", id, nl, nw, nc, newspace, tabchar);
}
should be:
while ((c = getchar()) != EOF)
{
++nc;
if(c == '\n')
++nl;
if(c == ' ' || c == '\n' || c == '\t')
state = OUT;
if(c == ' ')
newspace++;
if(c == '\t')
tabchar++;
else if(state == OUT)
{
state = IN;
++nw;
}
id = nl + 1;
}
printf("| %d | %d | %d | %d | %d | %d |\n", id, nl, nw, nc, newspace, tabchar);

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

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

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)
| 8 | 49 | 32 | 0 | |
| 8 | 49 | 32 | 0 | 1 |
| 8 | 49 | 32 | 0 | 1_ | (underscores are spaces)
| 7 | 49 | 32 | 0 | 1_ |
| 7 | 50 | 32 | 0 | 1_ |
| 7 | 50 | 32 | 0 | 1_2 |
| 7 | 50 | 32 | 0 | 1_2_ |
| 6 | 50 | 32 | 0 | 1_2_ |
| 6 | 51 | 32 | 0 | 1_2_ |
| 6 | 51 | 32 | 0 | 1_2_3 |
| 6 | 51 | 32 | 0 | 1_2_3_ |
| 5 | 51 | 32 | 0 | 1_2_3_ |
| 5 | 52 | 32 | 0 | 1_2_3_ |
| 5 | 52 | 32 | 0 | 1_2_3_4 |
| 5 | 52 | 32 | 0 | 1_2_3_4_ |
| 4 | 52 | 32 | 0 | 1_2_3_4_ |
| 4 | 53 | 32 | 0 | 1_2_3_4_ |
| 4 | 53 | 32 | 0 | 1_2_3_4_5 |
| 4 | 53 | 32 | 0 | 1_2_3_4_5_ |
| 3 | 53 | 32 | 0 | 1_2_3_4_5_ |
| 3 | 54 | 32 | 0 | 1_2_3_4_5_ |
| 3 | 54 | 32 | 0 | 1_2_3_4_5_6 |
| 3 | 54 | 32 | 0 | 1_2_3_4_5_6_ |
| 2 | 54 | 32 | 0 | 1_2_3_4_5_6_ |
| 2 | 55 | 32 | 0 | 1_2_3_4_5_6_ |
| 2 | 55 | 32 | 0 | 1_2_3_4_5_6_7 |
| 2 | 55 | 32 | 0 | 1_2_3_4_5_6_7_ |
| 1 | 55 | 32 | 0 | 1_2_3_4_5_6_7_ |
| 1 | 56 | 32 | 0 | 1_2_3_4_5_6_7_ |
| 1 | 56 | 32 | 0 | 1_2_3_4_5_6_7_8 |
| 1 | 56 | 32 | 0 | 1_2_3_4_5_6_7_8_ |
| 0 | 56 | 32 | 0 | 1_2_3_4_5_6_7_8_ |
| 0 | 57 | 32 | 0 | 1_2_3_4_5_6_7_8_ |
| 0 | 57 | 32 | 0 | 1_2_3_4_5_6_7_8_9 |
| 0 | 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 :...

Replace only the zeros that a user inputs in c

I want to replace only a particular number which is zero which the user has entered in to a different number. here is my code so far
#include <stdio.h>
#include <stdio.h>
int main() {
int figOutVar[9][9];
printf("please enter the figure me out numbers\n");
for(int i=0; i<9; i++) {
for(int j=0; j<9; j++) {
printf("enter %d %d: ",i+1,j+1);
scanf("%d",&figOutVar[i][j]);
}
}
printf("the figure me out question is: \n");
for(int i=0; i<9; i++) {
for(int j=0; j<9; j++) {
if (figOutVar[i] == 0) {
return true;
}
if(j!=0) {
printf(" | ");
}
if(j==3) {
printf(" | ");
}
if(j==6) {
printf(" | ");
}
printf("%d",figOutVar[i][j]);
}
printf("\n");
}
return 0;
}
When compiled with an input
0 | 4 | 0 | | 2 | 0 | 0 | | 5 | 0 | 3
3 | 0 | 0 | | 9 | 0 | 0 | | 7 | 1 | 0
7 | 6 | 0 | | 0 | 0 | 0 | | 0 | 0 | 0
5 | 0 | 1 | | 0 | 0 | 8 | | 9 | 0 | 0
0 | 0 | 0 | | 5 | 0 | 3 | | 0 | 0 | 0
0 | 0 | 4 | | 1 | 0 | 0 | | 6 | 0 | 2
0 | 0 | 0 | | 0 | 0 | 0 | | 0 | 9 | 4
0 | 8 | 3 | | 0 | 0 | 4 | | 0 | 0 | 5
4 | 0 | 5 | | 0 | 0 | 1 | | 0 | 8 | 0
I want to replace the zeros that the user input into a particular number or variable, the zeros represent blank spaces or unfilled answer.
Really looking forward for your help, and Thanks.
If your question is as simple as it looks, the answer should be too. Simply run a for, checking for zeros in your array.
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(figOutVar[i][j] == 0){
figOutVar[i][j] = //here you can replace the zero with something else
}
}
}
Or, if you don't want in fact replace the original value, you could just print something else when the code find a zero.

C programming : how to create a game board with tokens in each cell?

I am creating a game board with tokens on it for an exercise.
Its a 9x9 board which I have already created but I am having problems with adding the tokens into each cell. Whenever I add in my code for the tokens it screws up the format of my game board.
I am fairly new to C, so sorry if this might be a stupid question, but I have tried searching and fixing it myself but had no luck so far. Any help would be appreciated.
Here's my code for the gameboard:
int x,y;
for (x=0; x<9; x++)
{
printf (" +");
for (y=0; y<9; y++)
printf("---+");
printf ("\n%d", x+1);
printf (" |");
for (y=0; y<9; y++)
printf(" |");
printf ("\n");
}
printf (" +");
for (y=0; y<9; y++)
for (y=0; y<9; y++)
printf("---+");
printf ("\n");
This is my code to display the tokens:
for(x=0; x<9; x++)
{
printf(" ");
switch(board[y][x])
{
case PEG:
printf("o");
break;
case HOLE:
printf(".");
break;
case INVALID:
printf(" ");
}
}
I need "o" to display in all cells, and "." display only in the middle cell.
This is the board that I have created:
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
You can't first print the board outline/borders, then print the contents. You need to do them "interleaved", so that you don't mess up the current cursor position.
One way of doing this is to iterate for more than the board's dimensions, i.e. instead od 9x9, iterate over an 11x11 area, and use if to check if the current location is in the board or on the border.
Check the code below:
#include <stdio.h>
int main(void) {
int a[9][9];
int i,j,k;
for(i=0;i<9;i++)
printf("+---");
printf("+\n");
memset(a,0,sizeof(a));
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
if( i == 4 && i==j )
printf("| . ");
else
printf("| %d ",a[i][j]);
}
printf("|\n");
for(k=0;k<9;k++)
printf("+---");
printf("+\n");
}
return 0;
}
Output:
+---+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | . | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+---+

Resources