Solved

# bang bit manuipulation

Posted on 2003-02-19
Medium Priority
645 Views
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
Question by:Discomonkey
[X]
###### 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

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.

Banath
EE Moderator
0

LVL 1

Accepted Solution

TheBeaver earned 480 total points
ID: 7984436
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

LVL 8

Expert Comment

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

-Narendra
0

LVL 1

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

LVL 1

Author Comment

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

LVL 1

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
0

LVL 8

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!?

-Narendra
0

## Featured Post

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â€¦
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
Course of the Month13 days, 21 hours left to enroll