News for hackers. Stuff that matters.
Re:Do binary files contain more 1s or 0s? by tlb on Tuesday November 12, 2019 @03:29PM (#21517794)
The code ignores leading zero bits in each byte, which is why it gets the surprising result of more ones than zeros.
Re:Do binary files contain more 1s or 0s? by Enginerrrd on Tuesday November 12, 2019 @03:35PM (#21517869)
I wonder about fixed format headers and wrappers with a default bias too.
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.
Re:Do binary files contain more 1s or 0s? by oconnor663 on Tuesday November 12, 2019 @03:33PM (#21517845)
The reason you're measuring more 1's than 0's is this line:

  binary = bin(decimal)[2:]
That's because while `bin(0xff)` is indeed "0b11111111", `bin(0x00)` is "0b0". Python omits your leading zeros.

A smaller nit, these lines:

  hexadecimal = binascii.hexlify(byte)
  decimal = int(hexadecimal, 16)
can be just

  decimal = byte[0]
Re:Do binary files contain more 1s or 0s? by edent on Tuesday November 12, 2019 @03:40PM (#21517929)
D'oh! I swear I had a `.zfill(8)` in there. Looks like the whole thing is wrong. Oh well!
Re:Do binary files contain more 1s or 0s? by japaget on Tuesday November 12, 2019 @04:29PM (#21518355)
This page is devoid of content except for a single "_" character. (I viewed the page in Chrome, Safari, Edge, and Firefox with identical results.) What am I missing?
Re:Do binary files contain more 1s or 0s? by episteme on Tuesday November 12, 2019 @04:40PM (#21518455)
Guessing it was edited after the mistakes were pointed out
Re:Do binary files contain more 1s or 0s? by kazinator on Tuesday November 12, 2019 @03:51PM (#21518057)
There are at least two silly things in this code,

One is the hex conversion. Compare:

  >>> bin(int(binascii.hexlify(b"\x05"), 16))[2:]
  '101'
versus just:

  >>> bin(b"\x05"[0])[2:]
  '101'
Secondly, the code should just count the 1 bits; there is no need for a count of zeros.
Re:Do binary files contain more 1s or 0s? by h2odragon on Tuesday November 12, 2019 @03:39PM (#21517917)
It is of course vitally important that the 1's and the 0's even out, otherwise you might overflow the bit bucket and jam the tubes.

(This is actually a thing, encoding schemes like MFM and RLL in magnetic media ensure there aren't too many sequential 1 bits etc. See also "Grey codes")

Re:Do binary files contain more 1s or 0s? by twic on Tuesday November 12, 2019 @03:49PM (#21518033)
Re:Do binary files contain more 1s or 0s? by jandrese on Tuesday November 12, 2019 @03:47PM (#21518011)
I would assume < 0.5 on average since English strings are pretty common and the top bit is usually 0. Zero padding is fairly common in files too, while 1 padding is relatively rare.
Re:Do binary files contain more 1s or 0s? by imjasonmiller on Tuesday November 12, 2019 @03:42PM (#21517961)
> Consider a file which only contains the letter A. It has the following bits: 10000001

I thought A was 65, not 129?

Re:Do binary files contain more 1s or 0s? by isoprophlex on Tuesday November 12, 2019 @03:30PM (#21517808)
Jesus this is some dumb shit.

In python, the string returned by

    bin(decimal)
drops the leading zeros

    >>> bin(3)
    '0b11'
    >>> bin(513)
    '0b1000000001'
    >>> bin(1024)
    '0b10000000000'
I don't want to turn this into a personal attack or an overly snarky comment but it's really something to try and implement a bit counting algo by hand and then fail to observe the way bin() behaves
Re:Do binary files contain more 1s or 0s? by dang on Wednesday November 13, 2019 @01:18PM (#21527432)
> I don't want to turn this into a personal attack or an overly snarky comment

That's a fine intention, but then you shouldn't lead with "Jesus this is some dumb shit" or end with "it's really something to try and [...] fail." Doing it that way breaks the site guidelines. Would you mind reviewing them? https://news.ycombinator.com/newsguidelines.html

Note that the leading guideline in the "comments" section is Be kind.

Re:Do binary files contain more 1s or 0s? by isoprophlex on Sunday November 24, 2019 @01:14AM (#21618741)
Belated, but, yeah. You're right. I blew my top a bit there...
Re:Do binary files contain more 1s or 0s? by uwydr on Tuesday November 12, 2019 @03:35PM (#21517861)
There's a Linux application called "ent" that calculates the entropy of a file. I use it to see if it's worth it to compress a file before I ship it somewhere else.
Re:Do binary files contain more 1s or 0s? by gigama on Tuesday November 12, 2019 @05:19PM (#21518809)
Elegant solution, learned something new today, thanks for posting.

$ ent -bc <filename>