Search code examples
binaryasciinumber-formatting

How a computer understands the difference between a binary for a decimal and same binary for a character?


I will ask the question by example.

If I type 33 on my keyboard, it is converted to 100001 for the processor of computer. If I type ! (exclamation mark) on my keyboard, its again converted to 33 and hence converted to 100001

My question is: How a computer understands that first 100001 was for 33 and second 100001 was for exclamation mark. How it is differed internally?

How same binary representation is shown on output screen as 2 different outputs in terms of 33 and ! mark?


Solution

  • Let's imagine an application (like in your example) that has two input fields. One input field is meant to contain a number between 0 and 255 (inclusive) and the other is meant to hold a single character (let's say as defined in ASCII, so it can be in the range of 0 to 127).

    When you indicate that you're done entering data in one of those fields (be it by pressing enter or pushing some button on-screen) then the program will take the input text and parse it into the desired format.

    So for the first input field, it will calculate the result (let's say you entered 33, so the result is the number 33) and it'll store it in some memory location X. The byte at memory location X is now 33.

    For the second input field, there's no conversion necessary, it simply takes the first (and/or only) character of the input and transfers it to a memory location Y. Assuming you've entered ! the byte at memory location Y is now also 33.

    If you looked at the memory at the locations X and Y and had no further information, you wouldn't know what the 33 in each case represents: it's just a number, it has no "flag" or meta-data that tells you how to interpret it.

    But the program that wrote them there knows: It knows that the value at memory location X is to be interpreted as a number and the value at memory location Y is to be interpreted as a character. That's written in the structure or the code itself.

    In theory one could have some kind of tagged storage where together with any given piece of information you also have some meta-data that explains what that information is.

    Many OO-systems actually implement this in a way: when data of an object is stored in memory, it often * contains a header that describes the type of the object. For example in OpenJDK the object header will contain a reference to the class of the object. In this case (if you are able to interpret the header) you can tell how each part of the following memory region is to be interpreted, by looking at the class description.

    * but not always, OO systems without runtime type information exist.