bang bit manuipulation

Posted on 2003-02-19
Medium Priority
Last Modified: 2011-09-20
Hey experts!  I hope you are prepared for an onslaught of questions I have for you.

I'm doing bit level manipulations homework in C.  I am trying to basic manipulations using a limited amount of operations.  There are restrictions are in place to help confuse me.(haha)  I would orginally have loved to sit down and figure these out, but time is thin and I need a little help.(spent last couple of days finishing a huge project, this one is due at midnight)  The system is real simple, I have skeleton code, I just have to fill in the blanks so to speak.  You can consider these are puzzlers, because that is generally what they are really.  These are straight out of my code

in the comments you can see restrictions, overall goal, etc.

in place of return 2, of course something else is to be put there.

Rating is the difficulty of the problem from 1 to 4, 4 being hard.

So any help will be gladly appreciated... I have no partner for this project and my time is low, so I can only rely on the help of you guys. I will be trying my best to figure these out, I'll still give points even If I get the answer.

Here's one I already did....
**** Start Example****
 * bitXor - x^y using only ~ and & 
 *   Example: bitXor(4, 5) = 1
 *   Legal ops: ~ &
 *   Max ops: 14
 *   Rating: 2
int bitXor(int x, int y) {

    int z = ~x & y;
    int zz = x & ~y;
    int zzz = ~z & ~zz;
    zzz = ~zzz;

 return zzz;
**** End Example****

 * bang - Compute !x without using !
 *   Examples: bang(3) = 0, bang(0) = 1
 *   Legal ops: ~ & ^ | + << >>
 *   Max ops: 12
 *   Rating: 4
int bang(int x) {
  return 2;
Question by:Discomonkey
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 7984384
Experts- this is confirmed to be homework. Posting a complete solution is against the membership guidelines.

Hints may be offered, but no complete solutions or code please.

EE Moderator

Accepted Solution

TheBeaver earned 480 total points
ID: 7984436
bitXor ....
xor works by detecting the bits that have changed ie...
0011 XOR

1) Detect the 11 bits via ANDing the inputs
2) Detect the 00 bits via ANDing the inverted inputs
You now have the bits that have not changed.
3) Invert the results of 1) and 2) and then AND them together to get the XOR of the inputs

bang ...
assuming what you are after is a zero if the input has a value

Expert Comment

ID: 7985337
What will '!' do in bitwise manipulation? '~' will negate. Is '!' same as '~'!?

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 7985682
No. ! means 0 if the value is non-zero but 1 if the value is zero.

I am guessing that this is probably a trick question. ie...

int Bang(int x)
  if( x )
    return 0;
  return 1;

Another way would be to invert and then increment and see if you got the original result, ie (~x + 1) == x is only true for 0.

A third way could be to AND with 1 and then shift right once until the value is zero (true) or the result of the AND is 1 (FALSE). But then again this is just the first way screwed in the butt.

PS: How many more times are you considering asking that Xor question? I've answered it 3 or 4 times as is :)

Author Comment

ID: 7990363
thanks thebeaver, although I wish I didn't make these posts now that I know the rules about homework.  

Expert Comment

ID: 7995807
Yeah, I wasn't aware of that rule either and I don't fully agree with it. I mean, sure, if you got lazy student who just want to get their homework done, then that's bad. But, if a student is just drawing a blank and the teacher is not helpfull, then it should be OK for us to help them out as long as it done in an instructional way.

Anyways, cheers

Expert Comment

ID: 8006519
What is the demarkation between the two students AND how do you makeout what type of student you are talking to in Experts-Exchange!?


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

800 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