In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

I want to use bitwise operator--complement on an integer array. Here is my code:

#include <stdio.h>

int number[100];

unsigned char temp[100],complement[100];

main()

{

number[1]=23;

/*type cast integer*/

temp[1]=(unsigned char)(number[1]);

/*use complement operator*/

complement[1]=~temp[1];

printf("u%",complement[1]);

return 0;

}

I didn't get the correct result,.Is there anything wrong?

Thanks, --km

#include <stdio.h>

int number[100];

unsigned char temp[100],complement[100];

main()

{

number[1]=23;

/*type cast integer*/

temp[1]=(unsigned char)(number[1]);

/*use complement operator*/

complement[1]=~temp[1];

printf("u%",complement[1])

return 0;

}

I didn't get the correct result,.Is there anything wrong?

Thanks, --km

kxm try this:

#include <stdio.h>

int number[100];

main()

{

number[1]=23;

printf("%d", ~number[1]);

}

#include <stdio.h>

int number[100];

main()

{

number[1]=23;

printf("%d", ~number[1]);

}

complement[1] is of type unsigned char, so printf only pushes 8 significant bits as parameter on the stack; the '%u' tells printf to treat the parameter as an unsigned int (usually 32 significant bits). so try:

printf("%u", (unsigned int)(complement[i]));

If you're going to twiddle bits, do it on unsigned types.

Here's a safe bit-twiddler:

unsigned int bitwiseNot(unsigned int n)

{

return ~n;

}

Wow, that was a tough one. :-)

Furthermore, bear in mind that bytes don't necessarily have 8 bits. They have /at least/ 8 bits. To find out how many bits per byte in your implementation, print out the value of CHAR_BIT (in <limits.h>). Of course, it's 99 chances out of 100 that it'll be 8! :-)

The problem with twiddling bits on an int is that you are assuming your representation is twos complement. On ones complement machines, you'll get a totally different result. Hence my earlier admonition.

I certainly agree that changing u% to %u is likely to result in more effective display of the result. <grin>

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

that is change u% to %u, u will get the correct answer 232. If that is not the answer u r looking to, then u views r wrong.

Integer is of 2 bytes, when u assign an integer to a character, the character gets allocated the least significant byte. In integer 23, the 2 bytes r as follows

00000000 00010111

so the characters get the least significant byte 00010111. When u complement it the value will be 1110100, which is equivalent to 232 in decimal.

I hope u understood. If u want to take the complement of the whole integer, then the sign bit will be taken into account, and u will get a negative answer, since the sign bit gets complemented.