Thread: Is there any wrong?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    16

    Is there any wrong?

    I can not figure out why I am getting compling errors . can someone help? And it there another number to use for bitnot besides -1?
    Code:
    #include <stdio.h>
    
    int main()
    {
    int x, y;
    
    int bitOr(int x, int y)
    {
    return ~(~ x & ~ y);
    }
    
    int bitXOr(int x,int y)
    {
    return (x|y) & ~(x&y);
    }
    
    int bitNot(int x)
    {
    return  x ^ 0xffffffff;
    }
    
    int isNotEqual(int x, int y)
    {
    return (x^y);
    }

  2. #2
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    1. your paranthesis are unequal
    2.you are defining functions inside a function(main function)...this will not work unless you use gcc
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by uscuba2 View Post
    I can not figure out why I am getting compling errors . can someone help? And it there another number to use for bitnot besides -1?
    Code:
    #include <stdio.h>
    
    int main()
    {
        int x, y;
    
        int bitOr(int x, int y)
        {
            return ~(~ x & ~ y);
        }
    
        int bitXOr(int x,int y)
        {
            return (x|y) & ~(x&y);
        }
    
        int bitNot(int x)
        {
            return  x ^ 0xffffffff;
        }
    
        int isNotEqual(int x, int y)
        {
        return (x^y);
        }
    I corrected your indentation; Do you now see the problem?
    You should explain what you're trying to do too. It looks like you're trying to implement each bitwise operator using other bitwise operators. In that case, you've succeeded, apart from missing bitAnd.
    Instead of specifically writing 0xffffffff, you could just write ~0, however if the purpose is to avoid the ~ operator from within that function, then nevermind. You do understand that the constant must have all bits set right? Using any other value will give the wrong answer in some cases.
    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"

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Quote Originally Posted by creeping death View Post
    1. your paranthesis are unequal
    2.you are defining functions inside a function(main function)...this will not work unless you use gcc
    This is what I need to to do:

    Write 4 C functions, as specified in the table below, which emulate the C operators |, ^, ~, and !=. You may use only straightline code (i.e., no loops or conditionals)and a limited number of C operators .

    we are using gcc if I was not what would I remove?
    we are to run it in linux

    so I enter gcc -o hw hw.c to run
    Last edited by uscuba2; 04-11-2009 at 09:11 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by uscuba2
    Write 4 C functions, as specified in the table below, which emulate the C operators |, ^, ~, and !=. You may use only straightline code (i.e., no loops or conditionals)and a limited number of C operators .
    What is the set of operators that you are allowed to use?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Quote Originally Posted by laserlight View Post
    What is the set of operators that you are allowed to use?
    Function Operator that this function emulates Operators you may use
    for bitOr(int, int) = ~ &

    for bitXOr(int, int) = ~ & |

    for bitNot(int) = & ^ |

    for isNotEqual(int, int) = ~ & ^ |

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, okay. Anyway, the point is the your functions should not be defined in the main function. Move them outside (e.g., before, unless you want to provide function prototypes) of the main function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM