Results 1 to 5 of 5

Thread: Bit counting

  1. #1
    Join Date
    Nov 2005
    Posts
    9

    Question Unanswered: Bit counting

    Hello,

    Is there a function similar to the MySQL BIT_COUNT() on 64-bit values?
    If not, what would be the most efficient way to implement it in Informix?

    Thank you!

  2. #2
    Join Date
    Apr 2007
    Location
    Jundiai / SP - Brasil
    Posts
    311
    what this function does?
    ________________________________________
    César Inacio Martins
    Jundiai / SP - Brasil
    http://www.imartins.com.br/informix - em Português
    http://www.imartins.com.br/informix - English (translated by Google).
    ________________________________________

  3. #3
    Join Date
    Nov 2005
    Posts
    9
    Quote Originally Posted by ceinma
    what this function does?
    Just click on the link.

    I wrote wrote a function to do it on an MS-SQL server (here).
    I am not familiar with the Informix syntax, are there differences?
    Last edited by Alexoren; 06-07-08 at 10:17.

  4. #4
    Join Date
    Apr 2007
    Location
    Jundiai / SP - Brasil
    Posts
    311
    Hi Alex,
    Sorry my bad english...

    After look the function are you created on the MS-SQL Server, I need to confess I got a little lazy to understand...
    But, now looking for the function on the google I found the manual of MySQL, more easy to understand

    well, I wrote this ...
    Code:
    --drop function bit_string;
    --drop function bit_count;
    create function bit_string(pValue int) returning char(256);
      define vI int;
      define vX int;
      define vBit smallint;
      define vRes char(300);
      --set debug file to "/tmp/sp.out";
      --trace on;
      let vRes="";
      let vI=0;
      while vI <= 30
        let vX=pow(2,vI);
        let vBit=sysmaster:bitval(pValue,vX);
        let vRes=vBit||trim(vRes);
        let vI=vI+1;
      end while;
      return vRes;
    end function
    document "Return a string with bit from parameter pValue"
    ;
    grant execute on procedure bit_string to public
    ;
    
    
    create function bit_count(pValue int) returning smallint;
      define vI int;
      define vX int;
      define vBit smallint;
      define vRes smallint;
      let vRes=0;
      let vI=0;
      while vI <= 30
        let vX=pow(2,vI);
        let vBit=sysmaster:bitval(pValue,vX);
        let vRes=vBit+vRes;
        let vI=vI+1;
      end while;
      return vRes;
    end function
    document "Return a number with how much bits are active from parameter pValue"
    ;
    grant execute on procedure bit_count to public
    ;

    If you are using IDS 7.31 , you will need to adapt:
    - change :
    "create function" >>> "create procedure"
    "end function" >>> "end procedure"
    - and this, don't ask me why... if you not change will get error on the execution.
    "while vI <= 30" >>>> "while vI <= 29"
    ________________________________________
    César Inacio Martins
    Jundiai / SP - Brasil
    http://www.imartins.com.br/informix - em Português
    http://www.imartins.com.br/informix - English (translated by Google).
    ________________________________________

  5. #5
    Join Date
    Apr 2007
    Location
    Jundiai / SP - Brasil
    Posts
    311
    I forgot.. this is what they do:
    Code:
    execute function bit_count(29);
    
    (expression)
    
               4
    
    1 row(s) retrieved.
    
     execute function bit_string(29);
    
    
    (expression)  0000000000000000000000000011101
    
    1 row(s) retrieved.
    ________________________________________
    César Inacio Martins
    Jundiai / SP - Brasil
    http://www.imartins.com.br/informix - em Português
    http://www.imartins.com.br/informix - English (translated by Google).
    ________________________________________

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •