Bitwise operators

Thread Solved

Join Date: Sep 2009
Posts: 18
Reputation: jralexander137 is an unknown quantity at this point 
Solved Threads: 0
jralexander137 jralexander137 is offline Offline
Newbie Poster

Bitwise operators

 
0
  #1
Sep 21st, 2009
I am having trouble understanding how to implement these problems in C using bit operators. I understand the basic logic gates and when I work them out on paper they work just not code wise. Any insight/help would be great.

  1. /*
  2.  * bitXor - x^y using only ~ and &
  3.  * Example: bitXor(4, 5) = 1
  4.  * Legal ops: ~ &
  5.  * Max ops: 14
  6.  * Rating: 2
  7.  */
  8. int bitXor(int x, int y) {
  9. return ~((~((~y)&x))&(~((~x)&y)));
  10. }
  11.  
  12. /*
  13.  * bitAnd - x&y using only ~ and |
  14.  * Example: bitAnd(6, 5) = 4
  15.  * Legal ops: ~ |
  16.  * Max ops: 8
  17.  * Rating: 1
  18.  */
  19. int bitAnd(int x, int y) {
  20. return ~((~x)|(~y));
  21. }
  22.  
  23. /*
  24.  * isEqual - return 1 if x == y, and 0 otherwise
  25.  * Examples: isEqual(5,5) = 1, isEqual(4,5) = 0
  26.  * Legal ops: ! ~ & ^ | + << >>
  27.  * Max ops: 5
  28.  * Rating: 2
  29.  */
  30. int isEqual(int x, int y) {
  31. return !(x ^ y);
  32. }

This is the only one I can get working but I don't think I implemented correctly.

  1. /*
  2.  * isZero - returns 1 if x == 0, and 0 otherwise
  3.  * Examples: isZero(5) = 0, isZero(0) = 1
  4.  * Legal ops: ! ~ & ^ | + << >>
  5.  * Max ops: 2
  6.  * Rating: 1
  7.  */
  8. int isZero(int x) {
  9.  
  10. //FINISHED.
  11.  
  12. return !(x!=0);
  13.  
  14. }

And I can't use loops or conditionals.

Thanks Again.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Bitwise operators

 
0
  #2
Sep 21st, 2009
You don't understand the code or the code does not work properly? It seem to work properly for me.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 18
Reputation: jralexander137 is an unknown quantity at this point 
Solved Threads: 0
jralexander137 jralexander137 is offline Offline
Newbie Poster

Re: Bitwise operators

 
0
  #3
Sep 21st, 2009
Maybe its the test code that I have to use to test it is not working right?

I mean for what I have is the logic correct? I'd post the test code that was given but it's like 330+lines long.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Bitwise operators

 
0
  #4
Sep 21st, 2009
If I am not wrong, it is logically correct.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,358
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Bitwise operators

 
0
  #5
Sep 21st, 2009
Originally Posted by jralexander137 View Post
Maybe its the test code that I have to use to test it is not working right?

I mean for what I have is the logic correct? I'd post the test code that was given but it's like 330+lines long.
Some quickie test code for my "playing along at home":
#include <stdio.h>

/* 
 * 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 ~((~((~y)&x))&(~((~x)&y)));
}

/* 
 * bitAnd - x&y using only ~ and | 
 *   Example: bitAnd(6, 5) = 4
 *   Legal ops: ~ |
 *   Max ops: 8
 *   Rating: 1
 */
int bitAnd(int x, int y)
{
   return ~((~x)|(~y));
}

/* 
 * isEqual - return 1 if x == y, and 0 otherwise 
 *   Examples: isEqual(5,5) = 1, isEqual(4,5) = 0
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 5
 *   Rating: 2
 */
int isEqual(int x, int y)
{
   return !(x ^ y);
}

/*
 * isZero - returns 1 if x == 0, and 0 otherwise 
 *   Examples: isZero(5) = 0, isZero(0) = 1
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 2
 *   Rating: 1
 */
int isZero(int x)
{
   return !(x!=0);
}

int test_unary(const char *name, int (*f)(int), int value)
{
   int result = f(value);
   printf("%s(%d) = %d\n", name, value, result);
   return result;
}

int test_binary(const char *name, int (*f)(int, int), int x, int y)
{
   int result = f(x, y);
   printf("%s(%d,%d) = %d\n", name, x, y, result);
   return result;
}

#define printpair(x) #x,x

int main()
{
   test_binary(printpair(bitXor),  4, 5);
   test_binary(printpair(bitAnd),  6, 5);
   test_binary(printpair(isEqual), 5, 5);
   test_binary(printpair(isEqual), 4, 5);
   test_unary(printpair(isZero), 0);
   test_unary(printpair(isZero), 5);
   return 0;
}

/* my output
bitXor(4,5) = 1
bitAnd(6,5) = 4
isEqual(5,5) = 1
isEqual(4,5) = 0
isZero(0) = 1
isZero(5) = 0
*/
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 18
Reputation: jralexander137 is an unknown quantity at this point 
Solved Threads: 0
jralexander137 jralexander137 is offline Offline
Newbie Poster

Re: Bitwise operators

 
0
  #6
Sep 21st, 2009
I keep getting errors along these lines when I test bitAnd though these are the same general error type for the rest just a dif number for the should be parts.

Gives 2[0x]. Should be 0[0x0].

Then next line down.

Gives 2[0x2]. Should be -21474836[0x80000000].
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,358
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Bitwise operators

 
0
  #7
Sep 21st, 2009
For what inputs?
  1. int main()
  2. {
  3. test_binary(printpair(bitAnd), 0xC0000000, 0x80000000);
  4. return 0;
  5. }
  6.  
  7. /* my output
  8. bitAnd(-1073741824,-2147483648) = -2147483648
  9. */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 18
Reputation: jralexander137 is an unknown quantity at this point 
Solved Threads: 0
jralexander137 jralexander137 is offline Offline
Newbie Poster

Re: Bitwise operators

 
0
  #8
Sep 21st, 2009
Here is the code that will test my methods. This is given and should not be modified.

I have tried to attach it.
Last edited by jralexander137; Sep 21st, 2009 at 3:28 pm.
Attached Files
File Type: c btest.c (9.5 KB, 1 views)
File Type: c tests.c (676 Bytes, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 18
Reputation: jralexander137 is an unknown quantity at this point 
Solved Threads: 0
jralexander137 jralexander137 is offline Offline
Newbie Poster

Re: Bitwise operators

 
0
  #9
Sep 21st, 2009
I've worked out bitAnd and isEqual on paper and they should work but they don't when coded.

isEqual

  1. return (~x & y) >> 3;

bitAnd

  1. (~((~(x | (~y)) | ((~x) | y))) << 1
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 18
Reputation: jralexander137 is an unknown quantity at this point 
Solved Threads: 0
jralexander137 jralexander137 is offline Offline
Newbie Poster

Re: Bitwise operators

 
0
  #10
Sep 21st, 2009
Added template code for methods looks like you need in order to compile btest.
Attached Files
File Type: c bits.c (5.7 KB, 4 views)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC