As most of you might already know computer can only understand, store and process on a bit(0 and 1).
But why?
Well it turns out, two-valued signals can readily be represented, stored, and transmitted easily and more reliably than traditional base 10 number representation — for example, as the presence or absence of a hole in a punched card, as a high or low voltage on a wire, or as a magnetic domain oriented clockwise or counterclockwise, etc.
Morever, a single bit is usually not very helpful in representing anything meaningful in computer but once we combine a sequence of bits, we can represent any finite set. for example, we can encode bits into negative/nonnegative and floating numbers and also can encode the letters and symbols in a document. This is why(or maybe not) we use 8bits or a byte as the smallest addressable unit of memory. In binary notation, its value ranges from 00000000 to 11111111. When viewed as a decimal integer, its value ranges from 0 to 255 and in hexadecimal it ranges from 00 to FF. We usually write bit patterns as hexadecimals(0 - 9, A - F) because it is easy for us to convert to/from binary notations.
In C programming language, numeric constants starting with 0x or 0X are interpreted as being in hexadecimal. The characters ‘A’ through ‘F’ may be written in either upper - or lowercase. For example, we could write the number FA1D37B as 0xFA1D37B, as 0xfa1d37b, or even mixing upper - and lower case (e.g., 0xFa1D37b).
Okay lets cover one more thing,
Some CS terms ahead!!!
Virtual memory - A conceptual large array of bytes in memory(there is more to it but it will come later in the series)
Memory Address - A unique number used to identify a byte of memory.
Virtual memory space - The set of all possible memory addresses(more will come later).
Word size - The nominal size of pointer data or maximum size of virtual memory address.
Today's computers are of either 32-bit or 64-bit word size which means programs can have access a range from 0 to 2^32 - 1(incase of 32-bit) and 0 to 2^64 - 1(incase of 64-bit) of virtual addresses. Most of the programs compiled in 64-bit machines can be run on 32-bit machines as well but vice-versa is not true.
Computers and compilers support multiple data formats using different ways to encode data, such as integers and floating point, as well as different lengths. For example, many machines have instructions for manipulating single bytes, as well as integers represented as 2-, 4-, and 8-byte quantities. They also support floating-point numbers represented as 4- and 8-byte quantities. The exact numbers of bytes for some data types depends on how the program is compiled. I showed sizes for typical 32-bit and 64-bit programs below:
To avoid confusion between bitness of the machine and compiler settings, ISO C99 introduced some datatypes where datasizes are fixed regardless of compiler or machine settings. As you can see in the above screenshot, int32_t and int64_t are among thems. Using fixed-size integer types is the best way for programmers to have close control over data representations.
Before signing off - We should try to write code which can be portable to different machines and compilers. One aspect of portability is to make the program insensitive to the exact sizes of the different data types. With the transition to 64-bit machines, many hidden word size dependencies have arisen as bugs in migrating 32-bit programs to new machines. For example, many programmers historically assumed that an object declared as type int could be used to store a pointer. This works fine for most 32-bit programs, but it leads to problems for 64-bit programs because as discussed above the word size or pointer size of 32-bit and 64-bit machine will be different.
Okay. that's all for today. Thank you.
Ref: Computer Systems: A Programmer's perspective.
The above is the detailed content of Representing and Manipulating Information in modern computer - Part 1. For more information, please follow other related articles on the PHP Chinese website!