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

2.4k

u/captainAwesomePants Apr 28 '22

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

YES!

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

YES!

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

Good question. It's because it's really cheap to build a trillion tiny things that can reliably be either on or off, and it's much more expensive to build a bunch of things that can be in 10 different states. For example, if you wanted to store any number between 1 and 1,000,000, you could either do it with 6 counters that could store 10 values each, or you could do it with 20 counters that could store 2 values each, and it turns out that 20 counters storing 2 values each are much cheaper to make.

Is that how compression works?

No. It's the same amount of information either way. It takes less space to write it on paper in decimal, but it's still the same total amount of information.

These may be crazy questions

These are crazy IMPORTANT questions. "Oh my god information is just a number" is perhaps the foundational thought of computer science. It leads to Hilbert's "oh crap mathematical proofs can be a number" discovery, which led to Godel's incompleteness theorem, which is one of the most astounding, depressing, and important mathematical discoveries of the 20th century. It's a GREAT question to be thinking about.

760

u/Smooth_Measurement_1 Apr 28 '22

Thank you for the detailed answer and for making me feel like I'm not so stupid for asking that question.

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!

241

u/l_am_wildthing Apr 28 '22

every possible number combination exists somewhere in pi

Not to be pedantic but this is a misconception about the infinite-ness of pi... just because it is infinite and non-repeating doesnt mean it has every possible number. 1.101001000100001000001... is infinite and non-repeatable but doesnt contain every number. Pi is obviously different but the same applies

33

u/Sufficient_Drop5013 Apr 29 '22

It may very well be a misconception, but you can't prove it's not true.

30

u/nimbledaemon Apr 29 '22

If you start believing everything you can't prove to be false, you're going to end up believing a lot of bullshit.

-1

u/Sufficient_Drop5013 Apr 29 '22

Exactly, no one can prove that God doesn't exists, nor anyone can prove that he/she/it Does exists. In the end, it's just a matter of faith. So if I have to choose, I choose to have faith in pi.

9

u/nimbledaemon Apr 29 '22 edited Apr 30 '22

That's the thing, though, you don't have to choose. Just admit we don't know the answer, and if it really interests you do some academic research or whatever they call it in math, maybe you'll find the answer one way or another. But believing one or the other before doing so just means that if in reality the opposite is true, you just made the process a lot harder for yourself, and if you made any assumptions based on your belief then all of those are going to be wrong as well, and it just turns into this whole thing you have to deconstruct because you made a faulty assumption somewhere.

Software developers should know the importance of this since one of the most common problems with software is making faulty assumptions about what your code is doing or how it should work, which can cause huge problems down the line when a lot of your code depends on one bad piece. This is also why modularization is important as well, it reduces how much of your code can break if something goes wrong.

3

u/Sufficient_Drop5013 Apr 29 '22

I like the way you think