# How many bytes of memory is used just to "acknowledge" that a certain file is a .jpg?

I'm rather new to computers and how they work in the microscopic scale, but here's the little bit that I know.

Computers have lots of transistors in them, both in memory and processor, and each transistor has two states, on and off. They are translated to binary equivalents 0 and 1. All data is stored in computers as combinations of 0s and 1s. Each 0 or a 1 is called a bit, and 8 bits make up a byte. A single byte is too small so units like kilobytes, megabytes, and gigabytes are more common.

I know that .jpg images occupy some memory in the computer, usually in the orders of kilobytes. However, the computer must know that the 0s and 1s that make up the image are for the image, and the computer must acknowledge this. In order to inform the computer that these 0s and 1s are assigned for a .jpg image, there must be a few bytes of data to inform that the rest of the bytes are a part of the image file. How many bytes would just acknowledging the file type take?

Zero. A file type doesn't need to be “acknowledged”.

Imagine a photo of a tree. How much surface on the photo is needed to acknowledge where the photo was taken? Zero. The photo doesn't need to include an indication of where it was taken. It may do so, of course (for example, someone could have held a GPS's display in front of the tree), but equally it may not. There may be a legend below the photo, or here may not. And even if there's an indication, it could be false.

This metaphor works for a file's format.

Data in memory is just a bunch of bits. The semantics of this bunch of bits is defined by the code that manipulates it, not by the values of the bits alone. See How does a computer determine the data type of a byte?

If a location in memory may contains $n$ different file types, and the program needs to store an indication of the file type, then storing the type requires at least $\lceil \log_2(n)\rceil$ bits. This is because $k$ bits of memory can store $2^k$ different values; $\lceil \log_2(n)\rceil$ is the minimum value of $k$ such that $2^k \ge n$. In practice, it is common to use more bits (e.g. a whole word), because manipulating arbitrary groups of bits is not easy in a typical programming environment.

This is assuming that the type indication uses a constant amount of memory. If the type indication uses a variable amount of memory (e.g. a string), then the minimum is again 0 (but you then have the problem of how to store the length of the string). When data is stored in files, it's common to indicate the file format via the filename extension. But this is purely a convention; there is no mathematical relationship between a file's name and a file's content.

The JPEG format, like many other file formats, starts with a magic number. It's a 2-byte magic number. If a file begins with the two bytes (0xff,0xd8), then there's a good chance that it's a JPEG file. But this too is purely an engineering consideration — a convention that most programmers follow. It's a social observation, not a scientific observation.

• The file type doesn't have to be acknowledged? Then how does the computer know that this particular collection of bits codes for an image? What if the computer coded them into a text document instead? Aug 14 '17 at 15:44
• @PrittBalagopal It knows from contextual information. Read cs.stackexchange.com/questions/45794/… . It's unlikely with JPEG but some files do have multiple interpretations if you look at the content alone. Aug 14 '17 at 16:06
• I just tried to convert an image into a text document, by clicking Save As, and adding .txt to the file name. I got the text document littered with gibberish like "ÅÅÅÆ¢ç¥€€". Based on my little experiment, the computer has attempted to decode the bits into the wrong file type. Aug 14 '17 at 16:14
• @PrittBalagopal The computer has not “decoded” the bits. The content of the .jpg file and of the .txt file are identical. It's highly unlikely that the content of a JPEG file would make any sense if you view it as a text file. Aug 15 '17 at 16:37

Depends on how accurately you want it to be.

If you trust the extension then all you need is the 4 last characters in the file name (32 bits)

If you trust the magic header number then you need to check the file starts with 3 bytes ff d8 ff (24 bits). Though other file types have different sizes of magic number. There is a (non-exhaustive) list of them on wikipedia.

If you want to make sure the file is an actual image then you need to actually decode it. Here the size depends on the file size.

• +1 accurate answer. But the question seems computer engineering-ish to me. Aug 14 '17 at 9:03