Author Topic: Bitwise problem - Help!  (Read 4171 times)

0 Members and 1 Guest are viewing this topic.

Offline while1

  • x86
  • Hero Member
  • *****
  • Posts: 1013
    • View Profile
Bitwise problem - Help!
« on: September 25, 2007, 07:51:16 pm »
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

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Bitwise problem - Help!
« Reply #1 on: September 25, 2007, 07:53:36 pm »
How long is the word? I'm assuming 16, 32, or 64 bits?

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: Bitwise problem - Help!
« Reply #2 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.
« Last Edit: September 25, 2007, 08:50:48 pm by Camel »

<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!

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: Bitwise problem - Help!
« Reply #3 on: September 25, 2007, 08:54:21 pm »
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!

Offline while1

  • x86
  • Hero Member
  • *****
  • Posts: 1013
    • View Profile
Re: Bitwise problem - Help!
« Reply #4 on: September 25, 2007, 10:11:39 pm »
How long is the word? I'm assuming 16, 32, or 64 bits?
32

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

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: Bitwise problem - Help!
« Reply #5 on: September 25, 2007, 10:59:48 pm »
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!

Offline while1

  • x86
  • Hero Member
  • *****
  • Posts: 1013
    • View Profile
Re: Bitwise problem - Help!
« Reply #6 on: September 27, 2007, 09:21:14 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

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: Bitwise problem - Help!
« Reply #7 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? :)
« Last Edit: September 27, 2007, 09:54:56 pm by Camel »

<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!

Offline while1

  • x86
  • Hero Member
  • *****
  • Posts: 1013
    • View Profile
Re: Bitwise problem - Help!
« Reply #8 on: September 28, 2007, 05:24:36 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

Offline while1

  • x86
  • Hero Member
  • *****
  • Posts: 1013
    • View Profile
Re: Bitwise problem - Help!
« Reply #9 on: October 02, 2007, 11:50:45 am »
An example in terms of C/C++
Code: [Select]
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