• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 650
  • Last Modified:

bang bit manuipulation

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;
1 Solution
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
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
What will '!' do in bitwise manipulation? '~' will negate. Is '!' same as '~'!?

Industry Leaders: 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!

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 :)
DiscomonkeyAuthor Commented:
thanks thebeaver, although I wish I didn't make these posts now that I know the rules about homework.  
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
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now