Simple XOR Program

This is a discussion on Simple XOR Program within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int xor (int x, int y); int main() { int x; int y; printf ("Please enter the ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    76

    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.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by dolfaniss View Post
    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.
    Last edited by Subsonics; 05-20-2010 at 09:06 PM.

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by claudiu View Post
    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!!!

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    76
    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.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by dolfaniss View Post
    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.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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;
    }
    Last edited by Subsonics; 05-22-2010 at 04:28 PM.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    76
    Quote Originally Posted by Subsonics View Post
    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

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 10:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21