Solved

bitwise operator--complement

Posted on 2000-03-19
10
278 Views
Last Modified: 2010-04-15
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
 
 
0
Comment
Question by:kxm
10 Comments
 
LVL 2

Expert Comment

by:homer99
ID: 2633839
kxm try this:
#include <stdio.h>
int number[100];
main()
{
  number[1]=23;
  printf("%d", ~number[1]);
}
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 2633863
What result *are* you getting?
0
 
LVL 84

Expert Comment

by:ozo
ID: 2633908
changing "u%" to "%u" gets the correct result 232
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 1

Expert Comment

by:mahno
ID: 2635089
Ur program works correct:
23  = 00010111
232 = 11101000

0
 
LVL 2

Expert Comment

by:fremsley
ID: 2635417
i guess 'u%' was just a typo.

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]));

0
 
LVL 9

Expert Comment

by:Pacman
ID: 2636313
kxm, note that your code only converts the lo-byte of the integer.
0
 

Expert Comment

by:C_Dreamer
ID: 2656205
Note further that you shouldn't really muck about with the sign bit of a signed int.

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. :-)

0
 

Accepted Solution

by:
balakrishnan_t earned 50 total points
ID: 2656889
I feel that what ozo and fremsley said r correct.

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.
0
 

Expert Comment

by:C_Dreamer
ID: 2656949
An int is not necessarily two bytes. It's sizeof(int) bytes. Under Visual C++ 1.5, that gives you a value of 2. Under Visual C++ 5.0, it gives you a value of 4. On a Cray, sizeof(int) is 8.

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>
0
 

Author Comment

by:kxm
ID: 2657725
Thanks
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Picking random number 8 178
why debugging a macro i s difficult 10 38
Passing command line arguments in C 10 96
Constant string is of type char *   ? 7 42
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question