r/learnprogramming • • Apr 28 '22

If everything is ultimately binary, does't that just make programs a specific number?

I did the CS50 course a while ago and they covered binary in there. I did binary back in 2006 when I was at college too so understood the logic behind it already. It's either on or off!

When a C program compiles, it goes through it's phases and compiles to assembly and then ultimately machine code i.e binary.

Let's say our program is 5,000,000 pages of 1's and 0's. All of those 1's and 0's make up a single number....albeit a super long number.

Does that mean that the entire Linux operating system, for example, can be represented by an actual number?

If so, why are we not storing programs as a number because that would take up less space than the 5,000 pages of 1's and 0's.

110101000101011111011011111110111100 = 57000443836 in decimal. So why don't we store programs in decimal and convert that to 1's and 0's in real time?

Storage would be much less. Is that how compression works?

These may be crazy questions but it just entered my head earlier when thinking about what the binary of a program can represent.

Even shorter than decimal is HEX.... So why don't we store programs in Hex?

Any help appreciated on this one so I can wrap my head around it.

1.7k Upvotes

346 comments sorted by

View all comments

Show parent comments

5

u/mrbaggins Apr 29 '22

Entropy isn't necessarily the issue. You can quite simply define patterns that break many common algorithms. The point is that it compresses nicely in a different algorithm though.

4

u/linseed-reggae Apr 29 '22 edited Apr 29 '22

Entropy isn't necessarily the issue.

It absolutely is, information entropy is the formal definition of how much information a random variable has and Shannon went on to prove that there's an upper limit to that entropy for which any and all compression algorithms no longer actually shrink the data.

https://en.m.wikipedia.org/wiki/Shannon%27s_source_coding_theorem

You can quite simply define patterns that break many common algorithms.

Such as?

Because no, you can't. Unless we have different ideas of what "break" means in this context.

Any lossless data compression algorithm will always produce a valid output that can be successfully decoded.

Whether or not it's smaller than the original is a different question.

The point is that it compresses nicely in a different algorithm though.

No. Data with high enough Shannon entropy will not compress at all in any compression algorithm.

2

u/mrbaggins Apr 29 '22

Entropy absolutely IS an issue, I was trying to point out it isn't all issues.

You can quite simply define patterns that break many common algorithms.

Depends on which one you pick. Someone posted a simplified RLE before that picks up pairs. It's trivial to create a sequence that never repeats of 1s and 0s, and for length under some value, will never compress smaller than the original.

Because no, you can't. Unless we have different ideas of what "break" means in this context.

Any lossless data compression algorithm will always produce a valid output that can be successfully decoded

Yeah, different defa.i meant return an output larger or equal to input.

Whether or not it's smaller than the original is a different question.

Sure, but it's arguably a "break" in that your compression doesn't make it smaller

No. Data with high enough Shannon entropy will not compress at all in any compression algorithm.

Sure, but that's not the point I was making. It's interesting and not one I knew, that there's data that CANNOT be compressed, but it only serves to strengthen my original point, that for any algorithm it cannot compress EVERYTHING.

1

u/linseed-reggae Apr 29 '22 edited Apr 29 '22

Entropy absolutely IS an issue, I was trying to point out it isn't all issues.

You can quite simply define patterns that break many common algorithms.

Depends on which one you pick. Someone posted a simplified RLE before that picks up pairs. It's trivial to create a sequence that never repeats of 1s and 0s, and for length under some value, will never compress smaller than the original.

"a sequence that never repeats of 1s and 0s" you just described a binary representation of an (segment of) irrational number.

Irrational numbers by their very definition have no patterns within them. They have a very high entropy,

Also that's a horrible example because run length encoding handles stuff like that really well. I'm going to use A and B instead of 1 or 0 to distinguish between text characters and binary bits (because if the zeros and ones were meant to be bits then refer to above)

Given "AABBBAAAAABAAABBBB", RLE encoded that would be: "2A3B5AB3A4B". Point is RLE in particular handles those arbitrarily long sequences of the same symbol very well.

Edit I see now what you meant by RLE that picks up pairs. Creating a severely nerfed version of an already very trivial encoder to make to try to prove a point about data compression algorithms in general isn't convincing. Also it's not Run Length Encoding if it only picks up pairs lol.

Yeah, different defa.i meant return an output larger or equal to input.

Whether or not it's smaller than the original is a different question.

Sure, but it's arguably a "break" in that your compression doesn't make it smaller

I can see that logic.

It's interesting and not one I knew, that there's data that CANNOT be compressed, but it only serves to strengthen my original point, that for any algorithm it cannot compress EVERYTHING.

Learning about things you're interested in is always fun!

I don't want to take away the magic but this is a pretty mundane statement. The set of everything (that can be expressed in binary) is infinite, and includes every random number, every rational number, and every other incompressible type of data.

Data that cannot be compressed is not rare. Your CPU can generate many gigabytes per second of incompressible data - with the CSPRNG (random number) built into the hardware. An already compressed file also does not compress further.

In the grand scheme of what could be binary data, the stuff that actually is compressible is the exception and quite rare.

0

u/[deleted] Apr 29 '22

[deleted]

1

u/linseed-reggae Apr 29 '22

I think that what's they meant by "break".

I agree, but disagree that's "breaking" anything.

For known compression algorithms, you can construct an input with a simple pattern that triggers the worst case compression ratio for that algorithm. That input wouldn't necessarily have to be high entropy.

No. All data compression algorithms excel at finding patterns and reducing them. That's the cornerstone of how lossless compression algorithms are defined. They specifically target redundancy in data; which patterns, especially simple ones, are.

Furthermore, data compression algorithms don't formally define an upper bound for their "worst case", which means the final data could be doubled, tripled, or even larger. You will not find any "worst case" scenarios because that term is not defined in this context.

The more random data becomes, the less compressible it is.


Do you have any examples of a low entropy input breaking any compression algorithms? Even just one.

Because the closest I can think of is a zip bomb, which is conceptually the exact opposite of what you described: 42.zip is 42Kb large but decompresses to petabytes of data.

I think fundamentally you and the other guy don't understand what data compression algorithms are doing

0

u/[deleted] Apr 29 '22

[deleted]

1

u/linseed-reggae Apr 29 '22 edited Apr 29 '22

Because the closest I can think of is a zip bomb, which is conceptually the exact opposite of what you described: 42.zip is 42Kb large but decompresses to petabytes of data.

As I said, that isn't possible.

Lmao wut?!?

Are you categorically denying the existence of 42.zip?!?

https://en.wikipedia.org/wiki/Zip_bomb it's the second paragraph under details.

What an absurd position to take.

I studied computer science and while I'm no expert on compression, I think I have a pretty good grasp of it.

Judging by the consistent down votes your comments on /r/learnprogramming receive, I'm going to have to disagree with you here.

0

u/[deleted] Apr 29 '22

[deleted]

0

u/linseed-reggae Apr 29 '22

Obviously I'm talking about the reverse. A small input that will blow up in size when compressed.

So you're directly contradicting your own earlier comment.

you can construct an input with a simple pattern that triggers the worst case compression ratio for that algorithm.

1

u/linseed-reggae Apr 29 '22

A worst case obviously exists (as there does with any algorithm), but it is not as extreme as you assume. It just happens very often, even with lower entropy data.

You clearly don't understand what worst case means. Worst case is a rigorously defined mathematical property of an algorithm and to even begin talking about the worst case of something, it needs to be proven first.

0

u/[deleted] Apr 29 '22

[deleted]

1

u/linseed-reggae Apr 29 '22

But chill a bit, this is getting out of hand.

Dude I don't know what you're talking about, you posted wrong information and you're being corrected. Everyone makes mistakes but don't act like I'm the bad guy for correcting you.

1

u/linseed-reggae Apr 29 '22 edited Apr 29 '22

I wrote this python program to generate arbitrarily large files with a known upper bound for its entropy: the entropy is at most the size of the program, so 233 bytes. In reality it should be around 4 bytes (the size of the PRNG seed), but I'm no expert I'm going with the conservative estimate. The generated file input.dat will never compress well until you hit the period of the PRNG.

I don't know where you got 233 bytes of entropy total from, but that's nowhere even close to being correct.

The algorithm defined in your snippet is a pseudo random number generator, the only difference is that you're seeding it with a constant value of 3 instead of the system time.

There's numerous utilities out there to measure the entropy of a file.

When the output of your program is ran through an entropy calculator, the result is somewhere around 7.9 bits per byte. With a file size of 1048576 bytes, that file has 1,035,468 bytes of entropy, not 233 bytes.

Even if that entropy calculator was off by 50%, that's still 500 KB of entropy.

0

u/[deleted] Apr 29 '22

[deleted]

1

u/linseed-reggae Apr 29 '22 edited Apr 29 '22

Please explain to me how an PRNG generated more entropy than it was fed with.

That's literally exactly what PRNGs are designed and made for doing...

Pseudo random number generators, or PRNGs, are systems that are efficient in reliably producing lots of artificial random bits from a few true random bits. For example, a RNG which relies on mouse movements or keyboard key presses would stop working once the user stops interacting with the mouse or the keyboard. However a PRNG would use these random bits of initial entropy and continue producing random numbers.

https://www.redhat.com/en/blog/understanding-random-number-generators-and-their-limitations-linux

There's even software that's frequently used just for generating entropy to feed into the entropy pool: https://www.irisa.fr/caps/projects/hipsor/

It's been around for 20 years, the publications from the HAVEGE creators do a great job proving/reasoning that HAVEGE does indeed generate good entropy, as well as third-party analysis confirming those claims. http://jakob.engbloms.se/archives/1374

Also it's important I point out the distinction between information entropy (https://en.m.wikipedia.org/wiki/Entropy_(information_theory)) and entropy as used in cryptography contexts (https://en.m.wikipedia.org/wiki/Entropy_(computing))