News:

Help! We're trapped in the computer, and the computer is trapped in 2008! Someone call the time police!

Main Menu

Bitwise problem - Help!

Started by while1, September 25, 2007, 07:51:16 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

while1

A friend gave me this problem to work on and it's kind of pissing me off.  I've spent a few hours working on it but to no avail.

Basically you have to in a maximum of 40 operations (legal operations: ! ~ & ^ | + << >>) count the number of 1 bits of a word.
I tend to edit my topics and replies frequently.

http://www.operationsmile.org

iago

How long is the word? I'm assuming 16, 32, or 64 bits?

Camel

#2
I do believe theres an x86 instruction that does just that :)

A simple approach would be:
WORD theNumber = ?;
int bitCount = 0;

bitCount += ((theNumber) & 1);
bitCount += ((theNumber >> 1) & 1);
bitCount += ((theNumber >> 2) & 1);
bitCount += ((theNumber >> 3) & 1);
bitCount += ((theNumber >> 4) & 1);
bitCount += ((theNumber >> 5) & 1);
bitCount += ((theNumber >> 6) & 1);
bitCount += ((theNumber >> 7) & 1);
...


Although that will quickly use up your instructions.

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!

Camel

num - (num >> 1) - (num >> 2) ... (num >> sizeof(num)-1)

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!

while1

Quote from: iago on September 25, 2007, 07:53:36 PM
How long is the word? I'm assuming 16, 32, or 64 bits?
32

Quote from: Camel on September 25, 2007, 08:47:29 PM
I do believe theres an x86 instruction that does just that :)

A simple approach would be:
WORD theNumber = ?;
int bitCount = 0;

bitCount += ((theNumber) & 1);
bitCount += ((theNumber >> 1) & 1);
bitCount += ((theNumber >> 2) & 1);
bitCount += ((theNumber >> 3) & 1);
bitCount += ((theNumber >> 4) & 1);
bitCount += ((theNumber >> 5) & 1);
bitCount += ((theNumber >> 6) & 1);
bitCount += ((theNumber >> 7) & 1);
...


Although that will quickly use up your instructions.
Tried that, and since word size is 32-bits and each of those statements use 3 operations each (except the first one which uses 2), that goes way over the 40 operation limit. 
I tend to edit my topics and replies frequently.

http://www.operationsmile.org

Camel

Is this supposed to be an algorithm or a single equation?

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!

while1

Quote from: Camel on September 25, 2007, 10:59:48 PM
Is this supposed to be an algorithm or a single equation?
as long as your algorithm doesn't consist of any control structures (i.e. no if, for, etc.) and only the operations listed.
I tend to edit my topics and replies frequently.

http://www.operationsmile.org

Camel

#7
I think I just proved it's impossible. I'm going to stare at this a little bit longer to make sure I'm not wrong before I post it.

[edit] I'm assuming recursion isn't allowed? :)

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!

while1

Quote from: Camel on September 27, 2007, 09:52:35 PM
I think I just proved it's impossible. I'm going to stare at this a little bit longer to make sure I'm not wrong before I post it.

[edit] I'm assuming recursion isn't allowed? :)
right, it's not allowed.
I tend to edit my topics and replies frequently.

http://www.operationsmile.org

while1

An example in terms of C/C++

int bitCount(int x) {
      int var1 = Expr1;
      ...
      int varM = ExprM;

      varJ = ExprJ;
      ...
      varN = ExprN;
      return ExprR;
}


  Each "Expr" is an expression using ONLY the following:
  1. Integer constants 0 through 255 (0xFF), inclusive. You are
      not allowed to use big constants such as 0xffffffff.
  2. Function arguments and local variables (no global variables).
  3. Unary integer operations ! ~
  4. Binary integer operations & ^ | + << >>
   
  Some of the problems restrict the set of allowed operators even further.
  Each "Expr" may consist of multiple operators. You are not restricted to
  one operator per line.

  You are expressly forbidden to:
  1. Use any control constructs such as if, do, while, for, switch, etc.
  2. Define or use any macros.
  3. Call any functions.
  4. Use any other operations, such as &&, ||, -, or ?:
  5. Use any form of casting.

  You may assume that your machine:
  1. Uses 2s complement, 32-bit representations of integers.
  2. Performs right shifts arithmetically.
  3. Has unpredictable behavior when shifting an integer by more
     than the word size.
I tend to edit my topics and replies frequently.

http://www.operationsmile.org