954,190 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

counting digits

Does anyone knows how can I count how many digits are there in a binary number? Any ideas???

kiks
Newbie Poster
2 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 

You would need to convert the binary number to an integer first for that though, wouldn't you?

cscgal
The Queen of DaniWeb
Administrator
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 
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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 

I'm thinking of a binary number being stored as a string. Nevermind.

cscgal
The Queen of DaniWeb
Administrator
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 
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.

Ryu
Newbie Poster
24 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 
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 ?

vov4ik
Newbie Poster
16 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 
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:

mov ax, myBinaryValue
mov dx, 0000000000000001b  ; first ROR will make this most significant bit
mov cx, 16  ; 16 bits
 
LOOP:
ror   dx, 1   ; rotate bits to the right by 1
test  dx, ax ; this test will set ZF=0 (not zero) when the a bit is found on
jnz   BITFOUND
dec  cx       ; decrease bit count
jnz   LOOP  ; test again
 
;;;; if program enters here myBinaryValue is zero ;;;;
 
BITFOUND:
 
;;;; if program enters here a bit set on was found ;;;;
;;;; number of digits should be in CX register ;;;


Note: this program was never tested.

Ryu
Newbie Poster
24 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 
;
; Assuming 386 32 bit assembler code
;
   mov ecx,val 
   xor eax,eax  ; Counts the number of '1' bits

again:
   shl ecx,1
   adc eax,0
   or ecx,ecx
   jnz again

done:
;
; EAX has the count of bits in "val"
;
Purple Avenger
Light Poster
49 posts since Jan 2007
Reputation Points: 31
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You