In computer operation, everything is expressed with 0 and 1. By encoding/interpreting these sets of bits in various ways, computers make instructions and manipulate data types. Then why?

To understand the reason, should know the differences between digital and analog.

Analog represents data with a continuous physical amount(e.g. voltage, current). In contrast, Digital represents data with binary numbers. By expressing data in a digital way, we can transmit noisy and inaccurate data into classified values.

 

Computer represents 10 digit numbers into binary values. These are some examples of 2 number representation.

15213(10) = 11101101101101(2)

1.20(10) = 1.0011001100110011[0011]...(2)

1.5213 * 10^4(10) = 1.1101101101101(2) * 2^13

 

8 bits equals 1 Byte. So, we use Hexadecimal representation by using characters '0' to '9' and 'A' to 'F'.

00000000(2) ~ 11111111(2) = 0(10) ~ 255(10) = 00(16) ~ FF(16)

 

In C, the number of bytes differs by Data type. This is the number of bytes used in a typical 64-bit OS.

char = 1 byte

short = 2 bytes

int = 4 bytes

long = 8 bytes

float = 4 bytes

double = 8 bytes

pointer = 8 bytes

 

Manipulations, such as operations also exist in Bit-level.

Boolean Algebra, developed by George Boole, is an algebraic representation of logic. We encode 'True' as 1 and 'False' as 0.

  • $ (AND)
  • | (OR)
  • ~ (NOT)
  • ^ (Xor)

These operations can be applied to any integral data type (long, int, short, char, unsigned). Arguments are applied bit-wise.

 

By contrast, there are some logical operators which always return 0 or 1.

  • && (AND)
  • || (OR)
  • ! (NOT)
  • << (Left Shift)
  • >> (Right Shift)

0 is viewed as 'False', and anything nonzero is viewed as 'True'. Results are always 0 or 1. Early termination is applied to these operations.

 

Early termination?

In &&(AND), if one value is false, the result must be 0. So, if the first operand is 0(False), the computer does not calculate the second one.

In ||(OR), if one value is true, the result must be 1. So, if the first operand is 1(True), the result is True.

 

Example : Calculate p&&*p -> If p is empty(False), the computer does not calculate *p so that it avoids null pointer access.

 

+ Recent posts