• 0

C - Bit operations


Question

I've been trying to work out this problem for quite a long time. If anyone has any input, please help :p

it should return 0 if x is anything other than 0, and 1 if x is 0.

/* 
 * bang - Compute !x without using !
 *   Examples: bang(3) = 0, bang(0) = 1
 *   Legal ops: ~ & ^ | + << >>
 *   Max ops: 12
 *   Rating: 4 
 */
int bang(int x) {
	x =;
	return x;

}

Link to comment
Share on other sites

14 answers to this question

Recommended Posts

  • 0
Hint: Number XOR self = 0, always. X ^ X = 0.

To be honest, doesn't seem to help. Its obvious that x^x will always be 0. That doesn't check the value of x even a little.

Link to comment
Share on other sites

  • 0
To be honest, doesn't seem to help. Its obvious that x^x will always be 0. That doesn't check the value of x even a little.

XOR it with 0 then.

int bang(int x) {

	 if ((x ^ 0) == 0){
		return 1;
	 }else{
		return 0;
	 }

 }

edit: Simpler code

int bang(int x) {

	return ((x ^ 0) == 0);


}

I'm tired :p

This returns 1 if x = 0 and 1 if not.

Link to comment
Share on other sites

  • 0

I think his idea is to avoid using an if statement, but I feel like that's a bit too much of head scratching for saving 3 lines of code. Unless this needs to called a billion times per second, it shouldn't make any performance difference either.

Link to comment
Share on other sites

  • 0

Yeah that was the quick and dirty way I thought of doing it. Can be done in one line though, although as you say, it probably won't make any difference speed wise. Could always make it inline if it really mattered too.

Link to comment
Share on other sites

  • 0
XOR it with 0 then.

int bang(int x) {

	 if ((x ^ 0) == 0){
		return 1;
	 }else{
		return 0;
	 }

 }

edit: Simpler code

int bang(int x) {

	return ((x ^ 0) == 0);


}

I'm tired :p

This returns 1 if x = 0 and 1 if not.

even simpler!

int bang(int x) { return (x == 0) ? 1 : 0 }

but that defeats the purpose of the question

Link to comment
Share on other sites

  • 0

Here's another one that's very easy to understand, and it's exactly 12 ops:

int bang(int x) {
  x = x | (x >> 16);
  x = x | (x >> 8);
  x = x | (x >> 4);
  x = x | (x >> 2);
  x = x | (x >> 1);
  x = x & 0x00000001;
  return x ^ 1;
}

EDIT: Using a lookup table will eliminate a few ops.

Edited by MrA
Link to comment
Share on other sites

  • 0
I think his idea is to avoid using an if statement, but I feel like that's a bit too much of head scratching for saving 3 lines of code. Unless this needs to called a billion times per second, it shouldn't make any performance difference either.

There is no real point to the code other than learning. It's a homework question.

Link to comment
Share on other sites

  • 0
int bang(int x) {
  int xsign = x>>31;
  int negxsign = ((~x)+1)>>31;
  return (~(xsign|negxsign))&1;
}

Beat me to it. This seems to be the shortest solution (that uses only the legal ops mentioned in the post).

It relies on the fact that ZERO is the only number with identical sign in its +ve and -ve form (both are zero).

(sign | negsign) will be 0 for zero, and 1 for any other numbers. Flipping it, then and-ing it with 1 will give you the desired result.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.