| | |
Bitwise operators
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 26
Reputation:
Solved Threads: 0
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.
This is the only one I can get working but I don't think I implemented correctly.
And I can't use loops or conditionals.
Thanks Again.
c Syntax (Toggle Plain Text)
/* * 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.
c Syntax (Toggle Plain Text)
/* * 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.
•
•
•
•
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.
#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
For what inputs?
C Syntax (Toggle Plain Text)
int main() { test_binary(printpair(bitAnd), 0xC0000000, 0x80000000); return 0; } /* my output bitAnd(-1073741824,-2147483648) = -2147483648 */
"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
•
•
Join Date: Sep 2009
Posts: 26
Reputation:
Solved Threads: 0
I've worked out bitAnd and isEqual on paper and they should work but they don't when coded.
isEqual
bitAnd
isEqual
C Syntax (Toggle Plain Text)
return (~x & y) >> 3;
bitAnd
C Syntax (Toggle Plain Text)
(~((~(x | (~y)) | ((~x) | y))) << 1
![]() |
Similar Threads
- Enquiries about Bitwise operators (C++)
- Question Regarding "Bitwise Operators in C" (C)
- Code Snippet: Binary to decimal using bitwise operators (JavaScript / DHTML / AJAX)
- Diff b/w logical operators and bitwise operators...? (C)
- Bitwise operators/ undefined and implementation-defined? (C++)
- Problems with bitwise operators (C)
- Bitwise operators (C)
Other Threads in the C Forum
- Previous Thread: BGI Error:- Graphics not Initialized(Use Initgraph)
- Next Thread: Byte Reversal
Views: 440 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






