Usenet.com

www.Usenet.com

Group Index

Comp Thread Archive from Usenet.com

<-- __Chronological__ --> <-- __Thread__ -->

Re: Bitwise Complement Question




FYI Here is the example which returns the incorrect result (for the last printf statement (~x)....


#include <stdio.h>
main()
{
        int x, y, z;

        x = 4321;
        y = 5678;


printf("Given x = %u, i.e., 0X%04X\n", x, x); printf(" y = %u, i.e., 0X%04X\n", y, y);

z = x & y;

printf("x & y returns: %6u, i.e., 0X%04X\n", z, z);

z = x | y;

printf("x | y returns: %6u, i.e., 0X%04X\n", z, z);

z = x ^ y;

printf("x ^ y returns: %6u, i.e., 0X%04X\n", z, z);

printf(" ~x returns: %6u, i.e., 0X%04X\n", ~x,~x);

        return 0;
}


Steven B wrote:
I am a total beginner at C programming, and while following an example of using the bitwise complement operator, I got a very different result from that listed in the book.

The example shows the bitwise complement of integer x, (dec value 4321) as being 61214, hex value 0XEF1E. When I compile and run the example, I end up with the dec value 4294962974, hex value 0XFFFFEF1E.

The exercise includes examples of the other bitwise operators, all of which return the same results as the book when I run it.

I should mention that the book seems to lean towards Borland running on Windows -- I am using GCC 3.2.2 on RedHat. Is there something I should be learning about byte ordering or some such? I can't just ignore the problem and move on because I really want to understand whats going on, rather than just learning by rote.

Thanks for any insight.





<-- __Chronological__ --> <-- __Thread__ -->


Usenet.com



Please check out one of the premium Usenet Newsgroup Service Providers below for access to Usenet.