Thread: i have a question about bitwise operator?

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    11

    i have a question about bitwise operator?

    the program is : c program to check if two integers entered have opposite or same signs using bitwise operator

    when i decalre two varibles of type integer(int)
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<strings.h>
    
    
    
    int main(void)
    {
    
       int x=0,y=0;
       int result=0;
    
       printf("Enter the first number: ");
       scanf("%d",&x);
       printf("Enter the second number: ");
       scanf("%d",&y);
    
       result=x^y;
    
       if((result&0x80)==0x80)
        printf("\nthe two numbers are opposite\n");
       else
        printf("\nThe two numbers are same sign\n");
    
        return 0;
    }
    and the result was correct

    but when i change the type int of the two varibales into char type as that:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<strings.h>
    
    
    
    int main(void)
    {
    
       char x=0,y=0;
       char result=0;
    
       printf("Enter the first number: ");
       scanf("%d",&x);
       printf("Enter the second number: ");
       scanf("%d",&y);
    
       result=x^y;
    
       if((result&0x80)==0x80)
        printf("\nthe two numbers are opposite\n");
       else
        printf("\nThe two numbers are same sign\n");
    
        return 0;
    }
    the result Always "the two numbers are same sign"

    why the result not correct? and i thought that char type can store integer until 255..!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sweezy
    and the result was correct
    The code is wrong. int has a minimum guaranteed range of [-32767, 32767]. A quick test with 1234 and 2345 show a result of "the two numbers are opposite" when they clearly have the same sign.

    The bitwise xor means that if you have numbers with bit patterns like this:
    0110 0011 (decimal: 99)
    0110 0100 (decimal: 100)
    You end up with:
    0000 0111
    Then, bitwise and with 0x80 means you end up with:
    0000 0000
    hence the conclusion is that the numbers have the same sign, which is true. Likewise, if you try:
    1001 1101 (decimal: -99, i.e., from two's complement 8-bit binary)
    0110 0100 (decimal: 100)
    You end up with:
    1111 1001
    Then, bitwise and with 0x80 means you end up with:
    1000 0000
    hence the conclusion is that the numbers have different signs, which is true.

    But these conclusions are only incidentally correct. Consider:
    0100 1101 0010 (decimal: 1234)
    1001 0010 1001 (decimal: 2345)
    After bitwise xor:
    1101 1111 1011
    After bitwise and with 0x80:
    0000 1000 0000
    hence the conclusion is that the numbers have different signs, which is false.

    Quote Originally Posted by sweezy
    why the result not correct? and i thought that char type can store integer until 255..!
    char can be signed or unsigned. If char is signed, then its minimum guaranteed range is [-127, 127]. If char is unsigned, then its minimum guaranteed range is [0, 255]. Actually, the bitwise operations should work for a typical 8-bit signed char, i.e., it so happens that such a signed char would have just the right characteristics for the otherwise flawed assumption of 0x80 to work (regardless of the signed integer representation system, at least among the three permitted by the C standard). If you are working with unsigned char... other than the mathematical special case of whether zero has a sign, by definition "the two numbers are same sign" is always the case.
    Last edited by laserlight; 12-22-2015 at 12:54 AM.
    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. Using bitwise operator to see if x>y
    By Lina_inverse in forum C Programming
    Replies: 33
    Last Post: 09-23-2012, 07:16 PM
  2. Bitwise operator question
    By f1gh in forum C Programming
    Replies: 1
    Last Post: 12-25-2010, 12:31 PM
  3. Bitwise Operator Question/ Suggestion
    By ggraz in forum C Programming
    Replies: 4
    Last Post: 10-05-2008, 12:36 PM
  4. what does this mean, bitwise operator
    By InvariantLoop in forum C++ Programming
    Replies: 11
    Last Post: 03-09-2006, 07:43 PM
  5. bitwise operator
    By ssharish2005 in forum C Programming
    Replies: 8
    Last Post: 09-30-2005, 01:17 AM

Tags for this Thread