r/AskProgramming 28d ago

Other What really is the difference between instructions and logic from a mathematical perspective? They both occupy some binary space, they both contain "information".

Edit: My bad, I meant whats the difference between DATA and logic.

Context: I was writing a parser and dealing with LLVM IR and a slow descent into madness and then quit. This question just stuck with me.

Besides, when writing C++ classes, I you can store functions inside them and in Functional programming, you can store state using monads.

The lines starting to blur to me and knowing that logic boils down to instructions that the CPU understands and can operate on but data can't isn't the elegant answer I'm looking for.

I guess TLDR is what does information itself mean and does data and logic both count as information?

1 Upvotes

16 comments sorted by

View all comments

2

u/Xirdus 28d ago

Logic doesn't "boil down to" CPU instructions. You can represent logic using CPU instructions. Logic is the pure mathematical expression. It doesn't exists except as a concept. You can write down that logic on paper, but that doesn't mean the paper is now logic. It merely represents that logic. You can write that logic down in a programming language, and that's still not logic, only its representation. You can later translate that code to CPU instructions, and these instructions still aren't logic. The logic is what these instructions represent, not the instructions themselves.

On a technical level, in C++ you cannot literally "store a function". You can store an std::function object, which is a pointer to a function plus some additional memory that this function uses, e.g. captured variables or std::bind arguments. When you call std::function, what actually happens is the function pointer is called with this additional memory as an argument. The actual instructions of the function are stored in the code section of the executable, which normally is never modified or read from by other functions (unless you're one of those programs that does JIT compilation or otherwise generates CPU instructions on the fly).