If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Informix > Bit counting

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-03-08, 17:28
Alexoren Alexoren is offline
Registered User
 
Join Date: Nov 2005
Posts: 9
Question 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!
Reply With Quote
  #2 (permalink)  
Old 06-06-08, 09:03
ceinma ceinma is offline
Registered User
 
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).
________________________________________
Reply With Quote
  #3 (permalink)  
Old 06-07-08, 09:02
Alexoren Alexoren is offline
Registered User
 
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 09:17.
Reply With Quote
  #4 (permalink)  
Old 06-12-08, 14:44
ceinma ceinma is offline
Registered User
 
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).
________________________________________
Reply With Quote
  #5 (permalink)  
Old 06-12-08, 14:46
ceinma ceinma is offline
Registered User
 
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).
________________________________________
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On