# Simple XOR Program

• 05-20-2010
dolfaniss
Simple XOR Program
Code:

```#include <stdio.h> int xor (int x, int y); int main() {         int x;         int y;         printf ("Please enter the first number you'd like to XOR:\n");         scanf ("%x", &x);         getchar();         printf ("Please enter the second number you'd like to XOR:\n");         scanf ("%x", &y);         getchar();         printf ("The XOR value of the 2 numbers is: %x\n" ,xor (x,y));         getchar(); } int xor (int x, int y) {         return x ^ y; }```
How do I force the user to enter an 8 digit hex value?
Can I reject non-hex numbers in any position (ex. 444r5555, r is no good), I'm new so if this is too tough don't worry about it but I'd like to be able to do tghe first part.
• 05-20-2010
claudiu
You can't force the user to do anything. What you can do is verify that what he inputted is a valid input according to your specifications.

Also note that int main() should be int main(void) and should return 0 at the end of the function.
• 05-20-2010
Subsonics
Quote:

Originally Posted by dolfaniss
How do I force the user to enter an 8 digit hex value?
Can I reject non-hex numbers in any position (ex. 444r5555, r is no good), I'm new so if this is too tough don't worry about it but I'd like to be able to do tghe first part.

You can use bit mask to achieve that, example:

Code:

```        int mask = 0xf0000;         int x;         scanf("%x", &x);         if( x & mask)                 puts("Intolerable nibble, lol");         else                 printf("%x\n", x);```
This will look for "(ex. 444r5555, r is no good)" so, 0xfff0ffff is ok, but the fifth nibble must not be set.

But in your case, your probably better of by checking for "x & mask" in a while condition, so that the user will get stuck in that step until the correct input is left, but it's up to you.
• 05-21-2010
Kennedy
Quote:

Originally Posted by claudiu
You can't force the user to do anything.

Ah, but you are forgetting the e-gun. . .

::breaks into Chicago Italian Mafia accent:: Hey, you, yeah you, user! Come 'ere. You better input the data the right way or I'll mop da floor with cha!!!
• 05-21-2010
dolfaniss
OK I expanded my dumb little XOR program to ecept input and XOR numbers based on that input, actually kind of useful for work. But I'm still confused about testing the input to ensure its is 4 bytes and all valid hex numbers. I also feel the program, written by a real newbie, is probably extremely inefficient. Here is the code:

Code:

```#include <stdio.h> int xor (int x, int y); int main(void) {         int a;         int x;         int y;         int b;         int c;         int d;         int e;         int f;         int g;         printf ("How many addresses would you like to XOR? ");         scanf ("%d", &a);         getchar();                 while (a < 2 || a > 8)                 {                         printf ("Invalid entry please try again: ");                         scanf ("%d", &a);                         getchar();                 }                 if (a==2)                 {                         printf ("Please enter the first number you'd like to XOR: ");                         scanf ("%x", &x);                         printf ("Please enter the second number you'd like to XOR: ");                         scanf ("%x", &y);                         printf ("The XOR value of the 2 numbers is: %x\n" ,xor (x,y));                         getchar();                         return 0;                 }                 else if (a==3)                 {                         printf ("Please enter the first number you'd like to XOR: ");                         scanf ("%x", &x);                         printf ("Please enter the second number you'd like to XOR: ");                         scanf ("%x", &y);                         printf ("Please enter the third number you'd like to XOR: ");                         scanf ("%x", &b);                         printf ("The XOR value of the 3 numbers is: %x\n" ,xor1 (x,y,b));                         getchar();                         return 0;                 }                 else if (a==4)                 {                         printf ("Please enter the first number you'd like to XOR: ");                         scanf ("%x", &x);                         printf ("Please enter the second number you'd like to XOR: ");                         scanf ("%x", &y);                         printf ("Please enter the third number you'd like to XOR: ");                         scanf ("%x", &b);                         printf ("Please enter the forth number you'd like to XOR: ");                         scanf ("%x", &c);                         printf ("The XOR value of the 4 numbers is: %x\n" ,xor2 (x,y,b,c));                         getchar();                         return 0;                 }                 else if (a==5)                 {                         printf ("Please enter the first number you'd like to XOR: ");                         scanf ("%x", &x);                         printf ("Please enter the second number you'd like to XOR: ");                         scanf ("%x", &y);                         printf ("Please enter the third number you'd like to XOR: ");                         scanf ("%x", &b);                         printf ("Please enter the forth number you'd like to XOR: ");                         scanf ("%x", &c);                         printf ("Please enter the fifth number you'd like to XOR: ");                         scanf ("%x", &d);                         printf ("The XOR value of the 5 numbers is: %x\n" ,xor3 (x,y,b,c,d));                         getchar();                         return 0;                 }                 else if (a==6)                 {                         printf ("Please enter the first number you'd like to XOR: ");                         scanf ("%x", &x);                         printf ("Please enter the second number you'd like to XOR: ");                         scanf ("%x", &y);                         printf ("Please enter the third number you'd like to XOR: ");                         scanf ("%x", &b);                         printf ("Please enter the forth number you'd like to XOR: ");                         scanf ("%x", &c);                         printf ("Please enter the fifth number you'd like to XOR: ");                         scanf ("%x", &d);                         printf ("Please enter the sixth number you'd like to XOR: ");                         scanf ("%x", &e);                         printf ("The XOR value of the 6 numbers is: %x\n" ,xor4 (x,y,b,c,d,e));                         getchar();                         return 0;                 }                 else if (a==7)                 {                         printf ("Please enter the first number you'd like to XOR: ");                         scanf ("%x", &x);                         printf ("Please enter the second number you'd like to XOR: ");                         scanf ("%x", &y);                         printf ("Please enter the third number you'd like to XOR: ");                         scanf ("%x", &b);                         printf ("Please enter the forth number you'd like to XOR: ");                         scanf ("%x", &c);                         printf ("Please enter the fifth number you'd like to XOR: ");                         scanf ("%x", &d);                         printf ("Please enter the sixth number you'd like to XOR: ");                         scanf ("%x", &e);                         printf ("Please enter the seventh number you'd like to XOR: ");                         scanf ("%x", &f);                         printf ("The XOR value of the 7 numbers is: %x\n" ,xor5 (x,y,b,c,d,e,f));                         getchar();                         return 0;                 }                 else if (a==8)                 {                         printf ("Please enter the first number you'd like to XOR: ");                         scanf ("%x", &x);                         printf ("Please enter the second number you'd like to XOR: ");                         scanf ("%x", &y);                         printf ("Please enter the third number you'd like to XOR: ");                         scanf ("%x", &b);                         printf ("Please enter the forth number you'd like to XOR: ");                         scanf ("%x", &c);                         printf ("Please enter the fifth number you'd like to XOR: ");                         scanf ("%x", &d);                         printf ("Please enter the sixth number you'd like to XOR: ");                         scanf ("%x", &e);                         printf ("Please enter the seventh number you'd like to XOR: ");                         scanf ("%x", &f);                         printf ("Please enter the eighth number you'd like to XOR: ");                         scanf ("%x", &g);                         printf ("The XOR value of the 8 numbers is: %x\n" ,xor6 (x,y,b,c,d,e,f,g));                         getchar();                         return 0;                 } } int xor (int x, int y) {         return x ^ y; } int xor1 (int x, int y, int b) {         return x ^ y ^ b; } int xor2 (int x, int y, int b, int c) {         return x ^ y ^ b ^ c; } int xor3 (int x, int y, int b, int c, int d) {         return x ^ y ^ b ^ c ^ d ; } int xor4 (int x, int y, int b, int c, int d, int e) {         return x ^ y ^ b ^ c ^ d ^ e ; } int xor5 (int x, int y, int b, int c, int d, int e, int f) {         return x ^ y ^ b ^ c ^ d ^ e ^ f; } int xor6 (int x, int y, int b, int c, int d, int e, int f, int g) {         return x ^ y ^ b ^ c ^ d ^ e ^ f ^ g; }```
I'd like to say this is not a homework assignment, I've been following the tutorials on this site and writing some samples for practice. Any tips would be appreciated.
• 05-21-2010
tabstop
Quote:

Originally Posted by dolfaniss
OK I expanded my dumb little XOR program to ecept input and XOR numbers based on that input, actually kind of useful for work. But I'm still confused about testing the input to ensure its is 4 bytes and all valid hex numbers. I also feel the program, written by a real newbie, is probably extremely inefficient. Here is the code:

1) If they type something that's less than four bytes, is that a problem? The system will automatically put 0's in front, and everybody would be happy with that.
2) If they type something that's more than four bytes, is that a problem? Some ints are eight bytes these days. If your ints really are four bytes, then the extra would just get discarded as overflow.

You don't need to switch on n; you can use a loop to read numbers repeatedly. There is also such a thing as ^= that you will probably want to look into.
• 05-22-2010
Subsonics
Dolfaniss, if you need to confirm that the input is in the legal range i.e (0-9, a-f) then there is a function in ctype.h called isxdigit() that you can use it looks for (0-9, a-f, A-F).

You could also use a regex with scanf, or use strtol with argument 16, for hex. This all requires that you use a string as input though. The drawback of strtol alone is that it returns 0, it there was an error, hence you cant tell bogus input apart from 0. If that is necessary you need to first confirm that the string is ok, by, stripping of any newline character, advance the pointer beyond any 0x prefix of the string as isxdigit() does not permit x. then send the string to strtol for conversion to an int.

Example:

Code:

```#include <stdio.h> #include <string.h> #include <stdbool.h> #include <ctype.h> int main(void) {         char x[11], *xstr; //leaving room for null terminating character and eventual 0x prefix.         int i, num;         bool valid = true;         fgets(x, 11, stdin);         // check if new line char is present, if so, overwrite it with 0.         char *nl = NULL;         nl = strchr(x, '\n');         if(nl)                 *nl = 0;         // checking for 0x prefix, if present advance pointer.         xstr = x;         if(!memcmp(xstr, "0x", 2))                 xstr += 2;         // confirm that all chars are with in hex num range, if not set flag to false.         for(i = 0; i < strlen(xstr);i++){                 if(!(isxdigit(xstr[i])))                         valid = false;         }         // if flag is set to false, leave message and exit, else convert string to int.         if(!valid){                 puts("bogus input");                 return 0;         }         else                 num = (int)strtol(xstr, NULL, 16);         printf("0x%x\n", num);         return 0; }```
• 05-24-2010
dolfaniss
Quote:

Originally Posted by Subsonics
Dolfaniss, if you need to confirm that the input is in the legal range i.e (0-9, a-f) then there is a function in ctype.h called isxdigit() that you can use it looks for (0-9, a-f, A-F).

You could also use a regex with scanf, or use strtol with argument 16, for hex. This all requires that you use a string as input though. The drawback of strtol alone is that it returns 0, it there was an error, hence you cant tell bogus input apart from 0. If that is necessary you need to first confirm that the string is ok, by, stripping of any newline character, advance the pointer beyond any 0x prefix of the string as isxdigit() does not permit x. then send the string to strtol for conversion to an int.

Example:

Code:

```#include <stdio.h> #include <string.h> #include <stdbool.h> #include <ctype.h> int main(void) {         char x[11], *xstr; //leaving room for null terminating character and eventual 0x prefix.         int i, num;         bool valid = true;         fgets(x, 11, stdin);         // check if new line char is present, if so, overwrite it with 0.         char *nl = NULL;         nl = strchr(x, '\n');         if(nl)                 *nl = 0;         // checking for 0x prefix, if present advance pointer.         xstr = x;         if(!memcmp(xstr, "0x", 2))                 xstr += 2;         // confirm that all chars are with in hex num range, if not set flag to false.         for(i = 0; i < strlen(xstr);i++){                 if(!(isxdigit(xstr[i])))                         valid = false;         }         // if flag is set to false, leave message and exit, else convert string to int.         if(!valid){                 puts("bogus input");                 return 0;         }         else                 num = (int)strtol(xstr, NULL, 16);         printf("0x%x\n", num);         return 0; }```

Thanks
• 05-24-2010
iMalc
Lets say I'd like to XOR 100 numbers thanks. Wouldn't "Please enter the next number you'd like to XOR, or zero to stop" be a much better way of handling more than one?