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.
1.0k
u/captainAwesomePants Apr 28 '22 edited Apr 28 '22
No worries, this is really fun stuff. The whole "oh my god programs are data" revelation leads to a lot of amazing thoughts.
For example, pi is an infinite, nonrepeating decimal - meaning that every possible number combination exists somewhere in pi. Any program you write can be expressed as a decimal number. That means that your program exists somewhere in pi. Instead of storing the program itself, I could just tell you which digit of pi it starts on!
There was a controversy many years ago when someone published a small program that could decode DVDs. There were arguments and lawsuits basically claiming that the program was illegal. In response, some programmers found a prime number with 1401 digits that, in binary, was a gzip file containing the source code to the program. They argued that declaring a prime number illegal was ridiculous.
That stuff is fun and mind-blowing, but there are practical thoughts too. Once you realize that your program is a number, and you realize that programs are good at manipulating numbers, you'll probably realize that your program can do operations on programs, including on itself!