# How to calculate the Hamming weight of a binary string?

## 1 answer

+1

−0

I believe the answer at the moment is "No" but a very simple formula in LibreOffice 5.4.3.2 can achieve the desired result:

```
=LEN(SUBSTITUTE(B1;0;""))
```

where B1 contains:

```
=BASE(A1;2;8)
```

as valid and A1 contains the number in decimal.

The B1 cell would not be required with this version:

```
=LEN(SUBSTITUTE(BASE(A1;2;8);0;""))
```

This converts to binary, strips out the `0`

s and counts the number of characters left thereafter.

With the appropriate delimiter, should also work at least in Excel, Sheets and OpenOffice Calc.

## 7 comments

@Moshi Sorry, since posting this question I found out how to add tags not yet in the list and considered editing the tags more important than your suggestions. BTW I think OPs formula is

notredundant - it shows the appropriate separator. — pnuts 21 days ago@pnuts if all you need is what format the binary string is in, then it's easy enough to say, right? You could easily give an example, such as "10110111" or something, no need to be roundabout and say that it's the result of the BASE function — The Enraged Codidactyl 21 days ago

@Moshi No. What the OP's formula shows is use of

`;`

(rather than`,`

). — pnuts 21 days ago@pnuts ...does that...matter? — The Enraged Codidactyl 21 days ago

@Moshi Yes (unfortunately). Many users don't understand that the choice of separator may be optional (but critical for correct syntax). Answers have been down voted, useless edits made and users confused by mismatches. — pnuts 21 days ago

Show 2 more comments