Link to home
Start Free TrialLog in
Avatar of Discomonkey
Discomonkey

asked on

Bit level Manipulation

Hey experts!  I hope you are prepared for an onslaught of questions I have for you.

I'm doing bit level manipulations in C.  I am trying to basic manipulations using a limited amount of operations.

bitXor - x^y using only ~ and & 
    Example: bitXor(4, 5) = 1
    Legal ops: ~ &
    Max ops: 14
 

How do I program the BitXor with just ~ and &?  I am restricted to those two operations and and being able to create local variables and const values.  anyone got ideas?

I'll be handing out more nad more points, I have a ton and not afraid to dump them :)

**Update new question below, I answered my own question, I double the points though

 
 
Avatar of gj62
gj62

Sounds like homework - why the restrictions?  Have you any code you've started to work with?
Avatar of Discomonkey

ASKER

You are correct, this is homework.  The 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.  

This is straight out of my code...
*
 * bitXor - x^y using only ~ and & 
 *   Example: bitXor(4, 5) = 1
 *   Legal ops: ~ &
 *   Max ops: 14
 *   Rating: 2
 */
int bitXor(int x, int y) {

  return 2;
}

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.

ok I figured out my own question:

int bitXor(int x, int y) {

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

  return zzz;

but I have lots more
/*
 * isNotEqual - return 0 if x == y, and 1 otherwise
 *   Examples: isNotEqual(5,5) = 0, isNotEqual(4,5) = 1
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 6
 *   Rating: 2
 */
int isNotEqual(int x, int y) {
  return 2;
}
It is against the policies of this board for you to (a) ask homework questions and for (b) us to answer.  That said, generally if you have done a majority of the work and are stuck on a specific piece of code, some good soul will help you out.  However, nobody should post the answer to a question without seeing an attempt by you to answer.
Hi Discomonkey

gj62 is correct.
I'll monitor the Q to see no copy/paste answers are published.
Basically all info should come from your books !

modulo

Community Support Moderator
Experts Exchange
ASKER CERTIFIED SOLUTION
Avatar of akshayxx
akshayxx
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial