Thread: Help with a little piece of code

  1. #1
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49

    Help with a little piece of code

    Hey guys any ideas why this little piece of code is not outputing the results im expecting.

    Code:
    void And(char *Operand1,char *Operand2)
    
    {
    	int i;
    
    	printf("%20s","");
    
    	for(i=0;i<8;i++)
    	{
    	
    	if( (Operand1[i] && Operand2[i]) !='1')
    		putchar('0');
    
    	else
    		putchar('1');
    	
    	}
    		putchar('\n');
    
    	return;
    }
    Operand1 is '10111011' and Operand2 is '00111000'. The output is 00000000 not 00111000 which is what im expecting.

    Thanks
    Chris

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Operand1[i] && Operand2[i]
    The result of this is either 0 or 1
    Which is never the value '1'

    What you want is probably
    if ( Operand1[i] != '1' && Operand2[i] != '1' )

  3. #3
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49
    Still geting 00000000 even with those suggestions.

    Chris

  4. #4
    </life>
    Join Date
    Oct 2004
    Posts
    83
    If both characters equal to 1 then print out a one, else print out a zero...

    PHP Code:
    for(i=0;i<8;i++)

        if(
    Operand1[i] == '1' && Operand2[i] == '1'putchar('1'); 
        else 
    putchar('0');

    Microsoft is merely an illusion, albeit a very persistant one.

  5. #5
    </life>
    Join Date
    Oct 2004
    Posts
    83
    Here a mod of your original code, shown working in my terminal.

    PHP Code:
    bash-2.04cat ./oi.c
    #include <stdio.h>
    #include <stdlib.h>

    void And(char *Operand1,char *Operand2)
    {
            
    int i;

            for(
    i=0;i<8;i++)
            {
                    if(
    Operand1[i] == '1' && Operand2[i] == '1'putchar('1');
                    else 
    putchar('0');
            }
                    
    putchar('\n');
            return;
    }

    int main(void)
    {
    And(
    "10111011","00111000");

    return 
    0;
    }

    bash-2.04gcc oi.-o oi
    bash
    -2.04oi
    00111000
    bash
    -2.04
    Microsoft is merely an illusion, albeit a very persistant one.

  6. #6
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49
    Thanks guys I got it working.


    Chris

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whats wrong with this piece of code?
    By psycho88 in forum C Programming
    Replies: 3
    Last Post: 12-03-2005, 11:28 PM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  3. question about a piece of code
    By librab103 in forum C Programming
    Replies: 6
    Last Post: 06-27-2003, 01:11 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. What is your favorite piece of code?
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-22-2002, 07:12 AM