counting digits

Reply

Join Date: Dec 2006
Posts: 2
Reputation: kiks is an unknown quantity at this point 
Solved Threads: 0
kiks kiks is offline Offline
Newbie Poster

counting digits

 
0
  #1
Dec 14th, 2006
Does anyone knows how can I count how many digits are there in a binary number? Any ideas???
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: counting digits

 
0
  #2
Dec 14th, 2006
create a loop that repeatedly divides the value by 10 until its value becomes 0. increment a counter on each loop iteration. see the description for the idiv instruction if you do not know what it does.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,038
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: counting digits

 
0
  #3
Dec 14th, 2006
You would need to convert the binary number to an integer first for that though, wouldn't you?
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: counting digits

 
0
  #4
Dec 14th, 2006
Originally Posted by cscgal View Post
You would need to convert the binary number to an integer first for that though, wouldn't you?
binary numbers are either signed or unsigned integers. No conversion necessary.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,038
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: counting digits

 
0
  #5
Dec 14th, 2006
I'm thinking of a binary number being stored as a string. Nevermind.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 24
Reputation: Ryu is an unknown quantity at this point 
Solved Threads: 0
Ryu Ryu is offline Offline
Newbie Poster

Re: counting digits

 
0
  #6
Dec 17th, 2006
Originally Posted by kiks View Post
Does anyone knows how can I count how many digits are there in a binary number? Any ideas???
From what I understand, you want to count the number of digits in binary form, ex. 10001 (decimal 17) has 5 digits. You simply test the most significant towards least significant bits untill you reach a bit that is set on or 1. Total bits minus that count is the number of digits, example would be a 16bit register and a 0000000000010001 binary will give you 11 as your count (as theres 11 zeros). So 16-11 = 5. If you want the number of digits in base 10 then refer to the previous posts.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 16
Reputation: vov4ik is an unknown quantity at this point 
Solved Threads: 0
vov4ik vov4ik is offline Offline
Newbie Poster

Re: counting digits

 
0
  #7
Jan 4th, 2007
Originally Posted by Ryu View Post
From what I understand, you want to count the number of digits in binary form, ex. 10001 (decimal 17) has 5 digits. You simply test the most significant towards least significant bits untill you reach a bit that is set on or 1. Total bits minus that count is the number of digits, example would be a 16bit register and a 0000000000010001 binary will give you 11 as your count (as theres 11 zeros). So 16-11 = 5. If you want the number of digits in base 10 then refer to the previous posts.
How do i test the most/last significant bits of register?
In a general, is there list of all assembly functions available somewhere on web, i haven't found one ?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 24
Reputation: Ryu is an unknown quantity at this point 
Solved Threads: 0
Ryu Ryu is offline Offline
Newbie Poster

Re: counting digits

 
0
  #8
Jan 7th, 2007
Originally Posted by vov4ik View Post
How do i test the most/last significant bits of register?
In a general, is there list of all assembly functions available somewhere on web, i haven't found one ?
You use the TEST instruction, I'm assuming that you are working with IA32 architecture.

Example:

  1. mov ax, myBinaryValue
  2. mov dx, 0000000000000001b ; first ROR will make this most significant bit
  3. mov cx, 16 ; 16 bits
  4.  
  5. LOOP:
  6. ror dx, 1 ; rotate bits to the right by 1
  7. test dx, ax ; this test will set ZF=0 (not zero) when the a bit is found on
  8. jnz BITFOUND
  9. dec cx ; decrease bit count
  10. jnz LOOP ; test again
  11.  
  12. ;;;; if program enters here myBinaryValue is zero ;;;;
  13.  
  14. BITFOUND:
  15.  
  16. ;;;; if program enters here a bit set on was found ;;;;
  17. ;;;; number of digits should be in CX register ;;;

Note: this program was never tested.
Last edited by Ryu; Jan 7th, 2007 at 12:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 49
Reputation: Purple Avenger is an unknown quantity at this point 
Solved Threads: 0
Purple Avenger Purple Avenger is offline Offline
Light Poster

Re: counting digits

 
0
  #9
Jan 8th, 2007
  1. ;
  2. ; Assuming 386 32 bit assembler code
  3. ;
  4. mov ecx,val
  5. xor eax,eax ; Counts the number of '1' bits
  6.  
  7. again:
  8. shl ecx,1
  9. adc eax,0
  10. or ecx,ecx
  11. jnz again
  12.  
  13. done:
  14. ;
  15. ; EAX has the count of bits in "val"
  16. ;
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Assembly Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC