954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Bitwise operators

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.

/* 
 * 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);
}


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

/*
 * 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) {

//FINISHED.

  return !(x!=0);

}


And I can't use loops or conditionals.

Thanks Again.

jralexander137
Light Poster
33 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

You don't understand the code or the code does not work properly? It seem to work properly for me.

invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

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.

jralexander137
Light Poster
33 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

If I am not wrong, it is logically correct.

invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

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
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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

jralexander137
Light Poster
33 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

For what inputs?

int main()
{
   test_binary(printpair(bitAnd),  0xC0000000, 0x80000000);
   return 0;
}

/* my output
bitAnd(-1073741824,-2147483648) = -2147483648
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Here is the code that will test my methods. This is given and should not be modified.

I have tried to attach it.

Attachments btest.c (9.48KB) tests.c (0.66KB)
jralexander137
Light Poster
33 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

I've worked out bitAnd and isEqual on paper and they should work but they don't when coded.

isEqual

return (~x & y) >> 3;


bitAnd

(~((~(x | (~y)) | ((~x) | y))) << 1
jralexander137
Light Poster
33 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

Added template code for methods looks like you need in order to compile btest.

Attachments bits.c (5.69KB)
jralexander137
Light Poster
33 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

SOLVED.

Thanks everyone for your help with those concepts! All your input and advice really helped me! THANK YOU! <3! No homo. :)

jralexander137
Light Poster
33 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You