News for hackers. Stuff that matters.
Re:Do binary files contain more 1s or 0s? by Aardwolf on Tuesday November 12, 2019 @03:21PM (#21517718)
"2 / 8 = 0.29"

0.25 according to my calculations

pictures and /dev/random should give 0.5, not 0.57, if not they are not properly compressed and it's not a proper random generation, which I don't really believe. Maybe similar error as what happened with the 0.29?

The mean byte value in decimal should be 127.5.

I guess it may be something about the hexadecimal -> decimal -> binary conversion -> count instances of '0' and '1' in string in the python code that is doing something wrong (it's also inefficient to roundtrip through decimal when the original bytes are already binary in the computer and to involve strings in this at all). Does it remove leading zeros perhaps?

Re:Do binary files contain more 1s or 0s? by jeltz on Tuesday November 12, 2019 @03:35PM (#21517876)
The bug is that

    bin(decimal)[2:]
should be

    bin(decimal)[2:].rjust(8, '0')
or else the leading zeros are not counted.

Personally I would probably have used shift for a simple script like this.

Re:Do binary files contain more 1s or 0s? by joezydeco on Tuesday November 12, 2019 @03:38PM (#21517901)
Or a 255-entry precomputed lookup table.
Re:Do binary files contain more 1s or 0s? by fla on Tuesday November 12, 2019 @03:41PM (#21517945)
256 entries
Re:Do binary files contain more 1s or 0s? by joezydeco on Tuesday November 12, 2019 @03:43PM (#21517970)
You can skip the lookup on a zero and do the dereference on a non-zero value.
Re:Do binary files contain more 1s or 0s? by fla on Tuesday November 12, 2019 @03:47PM (#21518015)
You can, but that’ll cost you one extra branch, for nothing ;)
Re:Do binary files contain more 1s or 0s? by joezydeco on Tuesday November 12, 2019 @04:46PM (#21518512)
Sorry, was coming from an ARM perspective. Conditional execution is fun.
Re:Do binary files contain more 1s or 0s? by chmod775 on Tuesday November 12, 2019 @03:31PM (#21517823)
Looks like he accidentally typed "2 / 7" instead of "2 / 8" in a calculator, then rounded the result?
Re:Do binary files contain more 1s or 0s? by MByte on Tuesday November 12, 2019 @03:34PM (#21517859)
Yes, it's exactly that. The bin function removes leading zeroes.
Re:Do binary files contain more 1s or 0s? by jhoechtl on Tuesday November 12, 2019 @03:22PM (#21517731)
Well spoted. And ashaming for the OP
Re:Do binary files contain more 1s or 0s? by chmod775 on Tuesday November 12, 2019 @03:34PM (#21517858)
Mistakes like this happen to everyone, and are hardly something you need to be ashamed of.
Re:Do binary files contain more 1s or 0s? by noobermin on Tuesday November 12, 2019 @03:39PM (#21517910)
Tbh it helps to send articles to friends to look over before you go live and post to HN.
Re:Do binary files contain more 1s or 0s? by dang on Wednesday November 13, 2019 @01:17PM (#21527417)
Please let's not shame anybody here.