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

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

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

Problem:
/*
* 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;
}
0
Discomonkey
1 Solution

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

Banath
EE Moderator
0

Commented:
bitXor ....
xor works by detecting the bits that have changed ie...
0011 XOR
0101
====
0110

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
0

Commented:
Discomonkey,
What will '!' do in bitwise manipulation? '~' will negate. Is '!' same as '~'!?

-Narendra
0

Commented:
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 :)
0

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

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

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

-Narendra
0

## Featured Post

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