Skip to content

Instantly share code, notes, and snippets.

@kezzico
Last active November 9, 2023 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kezzico/8955f09f0b43e821e12ae6b8729ea90c to your computer and use it in GitHub Desktop.
Save kezzico/8955f09f0b43e821e12ae6b8729ea90c to your computer and use it in GitHub Desktop.

BINARIES

A binary is another term for a process. So, on POSIX systems, the term is used as a directory name. This naming convention lets others know that this is where to find the executable binary code.

Binary Code

Shebang

Not all binaries are actually binary. Some of them are scripts. Written in Python, Bash, or JavaScript. They are not yet compiled into the raw binary instructions fed into the CPU. These programs rely on an 'interpreter'. The interpreter makes the conversion at runtime. As the process runs the script is parsed and converted into binary code.

To identify which interpreter is needed by the script, a special character sequence is used called the shebang. The shebang is always the first line in the file, and it always starts with (#!).

Some sample Shebangs

#!/bin/bash 

#!/usr/local/bin/python

#!/Users/lee/.nvm/versions/node/v16.13.1/bin/node

To start the process, the shebang tells the kernel to use the interpreter binary and pass the file with the shebang as a parameter.

Native Code

To write binary code, use a native language like C, C++, Objective-C (but not C#).

Most systems have gcc or cc installed in their path, so it's easy to write hello world in C.

gcc hello-world.c

hello-world.c

#include <stdio.h>

int main() {
    printf("Hello World"); // it's a cultural reference
    
    return 0;
}

Java and C#, Bytecodes

Bytecode is not a script, and it's not a binary. It's somewhere inbetween. Bytecode has a compiler and an interpreter, bringing the best of both worlds. For C#, the DotNet framework interprets the bytecodes. For Java, it's the JVM. So there is still a compile step, like with native code, however the compiler does not compile into binary it compiles into bytecode

For example, to compile a java program into bytecode use javac.

javac hello-world.java

hello-world.java

// OOPS 
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

This produces a file containing the bytecode. To execute the bytecode output use java

java hello-world

Conclusion

Programs that we use every day come in all these different forms. Windows users may know them as .exe files. These are the 3 common types of executables, or binaries. Ultimately, the binary's purpose is to provide instructions to the CPU.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment