A Microsoft Office (Excel, Word) forum. OfficeFrustration

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   Home » OfficeFrustration forum » Microsoft Excel » General Discussion
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Binary Numbers



 
 
Thread Tools Display Modes
  #1  
Old January 21st, 2009, 02:10 PM posted to microsoft.public.excel.misc
Sanford Lefkowitz
external usenet poster
 
Posts: 10
Default Binary Numbers

This might be more of a math question than Excel.

I have an application for which I would like to know the number of "1"s in
the binary representation of a number. For example 20 in binary is 10100,
which has 2 "1"s. I do not necessarily need the binary number itself.

Also, the DEC2BIN function does not work for numbers =512. Are there other
binary converters that do?

Thanks
Sanford
  #2  
Old January 21st, 2009, 02:25 PM posted to microsoft.public.excel.misc
Bernie Deitrick
external usenet poster
 
Posts: 2,496
Default Binary Numbers

Sanford,

For numbers 512 and below:

=LEN(DEC2BIN(A1))-LEN(SUBSTITUTE(DEC2BIN(A1),"1",""))

For longer numbers, you could use a UDF - put this code into a standard codemodule:

Function BinCount(myVal As Long) As Integer
While myVal 0
If myVal Mod 2 = 1 Then BinCount = BinCount + 1
myVal = (myVal - myVal Mod 2) / 2
Wend
End Function

used like

=BinCount(A1)

HTH,
Bernie
MS Excel MVP


"Sanford Lefkowitz" wrote in message
...
This might be more of a math question than Excel.

I have an application for which I would like to know the number of "1"s in
the binary representation of a number. For example 20 in binary is 10100,
which has 2 "1"s. I do not necessarily need the binary number itself.

Also, the DEC2BIN function does not work for numbers =512. Are there other
binary converters that do?

Thanks
Sanford



  #3  
Old January 21st, 2009, 02:30 PM posted to microsoft.public.excel.misc
Gary''s Student
external usenet poster
 
Posts: 7,584
Default Binary Numbers

In A1 enter:

11011011001110010101001110101011101011
as text

in B1, enter:
=LEN(A1)-LEN(SUBSTITUTE(A1,"1","")) to display 23

--
Gary''s Student - gsnu200828


"Sanford Lefkowitz" wrote:

This might be more of a math question than Excel.

I have an application for which I would like to know the number of "1"s in
the binary representation of a number. For example 20 in binary is 10100,
which has 2 "1"s. I do not necessarily need the binary number itself.

Also, the DEC2BIN function does not work for numbers =512. Are there other
binary converters that do?

Thanks
Sanford

  #4  
Old January 21st, 2009, 02:36 PM posted to microsoft.public.excel.misc
Sanford Lefkowitz
external usenet poster
 
Posts: 10
Default Binary Numbers

clever technique

Thanks!!


"Bernie Deitrick" wrote:

Sanford,

For numbers 512 and below:

=LEN(DEC2BIN(A1))-LEN(SUBSTITUTE(DEC2BIN(A1),"1",""))

For longer numbers, you could use a UDF - put this code into a standard codemodule:

Function BinCount(myVal As Long) As Integer
While myVal 0
If myVal Mod 2 = 1 Then BinCount = BinCount + 1
myVal = (myVal - myVal Mod 2) / 2
Wend
End Function

used like

=BinCount(A1)

HTH,
Bernie
MS Excel MVP


"Sanford Lefkowitz" wrote in message
...
This might be more of a math question than Excel.

I have an application for which I would like to know the number of "1"s in
the binary representation of a number. For example 20 in binary is 10100,
which has 2 "1"s. I do not necessarily need the binary number itself.

Also, the DEC2BIN function does not work for numbers =512. Are there other
binary converters that do?

Thanks
Sanford




  #5  
Old January 21st, 2009, 02:52 PM posted to microsoft.public.excel.misc
Bernie Deitrick
external usenet poster
 
Posts: 2,496
Default Binary Numbers

Actually, it's sloppy - I should have changed this:

myVal = (myVal - myVal Mod 2) / 2

to this:

myVal = myVal \ 2

HTH,
Bernie
MS Excel MVP


"Sanford Lefkowitz" wrote in message
...
clever technique

Thanks!!


"Bernie Deitrick" wrote:

Sanford,

For numbers 512 and below:

=LEN(DEC2BIN(A1))-LEN(SUBSTITUTE(DEC2BIN(A1),"1",""))

For longer numbers, you could use a UDF - put this code into a standard codemodule:

Function BinCount(myVal As Long) As Integer
While myVal 0
If myVal Mod 2 = 1 Then BinCount = BinCount + 1
myVal = (myVal - myVal Mod 2) / 2
Wend
End Function

used like

=BinCount(A1)

HTH,
Bernie
MS Excel MVP


"Sanford Lefkowitz" wrote in message
...
This might be more of a math question than Excel.

I have an application for which I would like to know the number of "1"s in
the binary representation of a number. For example 20 in binary is 10100,
which has 2 "1"s. I do not necessarily need the binary number itself.

Also, the DEC2BIN function does not work for numbers =512. Are there other
binary converters that do?

Thanks
Sanford






  #6  
Old January 21st, 2009, 06:03 PM posted to microsoft.public.excel.misc
James Silverton[_4_]
external usenet poster
 
Posts: 162
Default Binary Numbers

Bernie wrote on Wed, 21 Jan 2009 09:25:11 -0500:

For numbers 512 and below:


=LEN(DEC2BIN(A1))-LEN(SUBSTITUTE(DEC2BIN(A1),"1",""))


For longer numbers, you could use a UDF - put this code into a
standard codemodule:


Function BinCount(myVal As Long) As Integer
While myVal 0
If myVal Mod 2 = 1 Then BinCount = BinCount + 1
myVal = (myVal - myVal Mod 2) / 2
Wend
End Function


used like


=BinCount(A1)


That looks like the way to go but shouldn't BinCount be initialized to 0
?

--

James Silverton
Potomac, Maryland

Email, with obvious alterations: not.jim.silverton.at.verizon.not

  #7  
Old January 21st, 2009, 06:20 PM posted to microsoft.public.excel.misc
Bernie Deitrick
external usenet poster
 
Posts: 2,496
Default Binary Numbers

James,

BinCount is a function name and not a variable, so it is reset to null (zero) every time the
function is called. The only time you need to initialize a function's value is when there is a
default return that is not null/zero - it can't hurt to use BinCount = 0 as the first line, but it
isn't needed.

HTH,
Bernie
MS Excel MVP


"James Silverton" wrote in message
...
Bernie wrote on Wed, 21 Jan 2009 09:25:11 -0500:

For numbers 512 and below:


=LEN(DEC2BIN(A1))-LEN(SUBSTITUTE(DEC2BIN(A1),"1",""))


For longer numbers, you could use a UDF - put this code into a
standard codemodule:


Function BinCount(myVal As Long) As Integer
While myVal 0
If myVal Mod 2 = 1 Then BinCount = BinCount + 1
myVal = (myVal - myVal Mod 2) / 2
Wend
End Function


used like


=BinCount(A1)


That looks like the way to go but shouldn't BinCount be initialized to 0 ?

--

James Silverton
Potomac, Maryland

Email, with obvious alterations: not.jim.silverton.at.verizon.not



  #8  
Old January 21st, 2009, 06:33 PM posted to microsoft.public.excel.misc
James Silverton[_4_]
external usenet poster
 
Posts: 162
Default Binary Numbers

Bernie wrote on Wed, 21 Jan 2009 13:20:21 -0500:

BinCount is a function name and not a variable, so it is reset
to null (zero) every time the function is called. The only
time you need to initialize a function's value is when there
is a default return that is not null/zero - it can't hurt to
use BinCount = 0 as the first line, but it isn't needed.


HTH,
Bernie
MS Excel MVP



That looks like the way to go but shouldn't BinCount be
initialized to 0 ?

Thanks for the reply. Obviously, I am no expert on VBA but I was taught
to initialize variables in other programming languages.

--

James Silverton
Potomac, Maryland

Email, with obvious alterations: not.jim.silverton.at.verizon.not

  #9  
Old May 12th, 2010, 03:01 PM posted to microsoft.public.excel.misc
Reg
external usenet poster
 
Posts: 71
Default Binary Numbers

elegant!


"Gary''s Student" wrote:

In A1 enter:

11011011001110010101001110101011101011
as text

in B1, enter:
=LEN(A1)-LEN(SUBSTITUTE(A1,"1","")) to display 23

--
Gary''s Student - gsnu200828


"Sanford Lefkowitz" wrote:

This might be more of a math question than Excel.

I have an application for which I would like to know the number of "1"s in
the binary representation of a number. For example 20 in binary is 10100,
which has 2 "1"s. I do not necessarily need the binary number itself.

Also, the DEC2BIN function does not work for numbers =512. Are there other
binary converters that do?

Thanks
Sanford

  #10  
Old May 12th, 2010, 04:06 PM posted to microsoft.public.excel.misc
Bernd P
external usenet poster
 
Posts: 613
Default Binary Numbers

On 12 Mai, 15:01, Reg wrote:
elegant!



"Gary''s Student" wrote:
In A1 enter:


11011011001110010101001110101011101011
as text


in B1, enter:
=LEN(A1)-LEN(SUBSTITUTE(A1,"1","")) to display 23


--
Gary''s Student - gsnu200828


"Sanford Lefkowitz" wrote:


This might be more of a math question than Excel.


I have an application for which I would like to know the number of "1"s in
the binary representation of a number. For example 20 in binary is 10100,
which has 2 "1"s. I do not necessarily need the binary number itself.


Also, the DEC2BIN function does not work for numbers =512. Are there other
binary converters that do?


Thanks
Sanford- Zitierten Text ausblenden -


- Zitierten Text anzeigen -


=LEN(SUBSTITUTE(A1,"0",""))
 




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 10:02 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.