r/learnprogramming • u/Smooth_Measurement_1 • 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.
51
u/captainAwesomePants Apr 29 '22
Yes.
I'll take it that you completely understand that files are just bits and bytes, and bits and bytes can represent anything, including one giant number. So you understand that a large number can just be a representation of a file, and it follows naturally that one could make a gzip file containing the source code and convert that into a number like the one above.
Your confusion then is how the hell somebody could specifically aim to make that number be prime. That's a great thing to wonder about, and the answer is very clever. You see, gzip is special because if you add a few bytes to the end of it, nothing changes. It still decodes to the same thing. The bytes at the end are completely ignored. So, let's say you start with the gzip file you want, and then you add 2 bytes of zeroes at the end. Now turn that into a number. Now do this:
Check if the number is prime. If so, we did it! Stop.
Increase number by 1.
Goto 1
Pretty good chance that one of the next 65536 or so numbers is gonna be prime.
Here's the creator's explanation of how he worked it out: http://fatphil.org/maths/illegal1.html