首页 > 科技 > Review: C program

Review: C program

2007年8月21日 21点41分 发表评论 阅读评论

Manipulating Bits:
Computer data and files are made of bits(or bytes).

There’re six bit-manipulation operators in C:
&: The bitwise AND operation
|: The bitwise OR operation
^: The bitwise exclusive OR(XOR)
~: The bitwise complement operator
>>: right-shift operator
<<: left-shift operator Decimal number converts to Hexadecimal or Binary. Each digit in a hex number consists of fours bits. A bit represents a digit in a binary number. As we know, the binary is a 2-based numbering system. Each digit in a binary number is called a bit, and can be 1 or 0. If the position of a bit in a binary number is n, then the bit can have a value of 2 to the power of n. The position of a bit in a binary number is counted from the right of the binary number. The most-right bit is at the position of zero. For example: (Binary) 1000 -> 1*2**3+0*2**2+0*2**1+0*2**0 -> 8 (Decimal)
(Decimal) 10 -> 1*2**3+0*2**2+1*2**1+0*2**0 -> 1010 (Binary)

&: Compare each bit of x to the corresponding bit in y. If both bits are 1, 1 is placed at the same position of the bit in the result. If one of the bits, or two of them, is 0, 0 is placed in the result.
eg: 01 & 11 returns 01

|: However, places 1 in the result if either operand is 1.
eg: 01 | 11 returns 11

^: Place 1 in the result if either operand, but not both.
eg: 01 ^ 11 returns 10

~: Takes just one operand. This operator reverses each bit in the operand.
eg: ~01 returns 10

>>: Shifts the bits of and operand to the right.

<<: Operand shifts the bits to the left. Note that the unsigned integer format specifier with a minimum field width of 6, %6u, and the uppercase Hex format specifier with the minimum width of 4, %04X, and used in the printf() function. The unsigned integer data type(that is, the non-negative integer data type) is chosen so that the complementary value of an integer can be shown and understood easily. ?: is called condition operator x?y:z, Here x, y and z are three expressions. X contains the test condition. Y and Z present the final value of the expression, if X returns nonzero(TRUE), y is chosen; if X returns zero(FALSE), Z is chosen. ~(1< 1111 1100
3. Finally, add one to the complemented binary.
eg. 1111 1100 + 0000 0001 —-> 1111 1101

As shown above, -3 is represented as “1111 1101” in binary format. There’s another method to find its binary format(Also contains three steps):
1. Set sign bit at leftmost position, 1 represent negative, 0 represent integer:
eg. -3 —-> 1000 0011
2. Reverse all the bits except of sign bit.
eg. 1000 0011 —-> 1111 1100
3. Add 1 to the above binary format.
eg. 1111 1100 + 0000 0001 —-> 1111 1101

Compare with above two method, their results are same. (-3 has binary format as 1111 1101).

Note the follow cases:

Case One. 4 – 3 = 1
4 —-> 0000 0100
-3 —-> 1111 1101
——————–
+ (1)0000 0001 Here, the blue marked “1” is ignored.
Note, integer(indicates number > 0) has three same format(orginal,reverse,complement codes). See the result “0000 0001”,its left-most bit is set as 0, which means it is an integer(>0), and the corresponding decimal number is 1.
More attention, the sign bits of both 4 and (-3) must be took part in adding calculation.

Case Two. 1 – 2 = -1
-2 original code: 1000 0010
reverse code: 1111 1101
complement code: 1111 1101 + 0000 0001 —> 1111 1110
Hence, 1 —> 0000 0001
-2 1111 1110
———————-
+ 1111 1111 (Complement code)
Convert negative number 1111 1111 from its complement code to original code:
Complement Code: 1111 1111
Reverse Code: 1111 1111 – 0000 0001 = 1111 1110
Original Code: 1111 1110 —> 1000 0001 —> Decimal 10
Here, when convert negative number from reverse code to its original code, the sign bit must NOT be changed.

Case Three: -2 – 2 = -4
-2 Complement Code: 1111 1110
-2 1111 1110
—————————–
+ Result Comple. Code: (1)1111 1100

Here blue marked 1 is ignored.

Convert the result to decimal:
Complement Code: 1111 1100
Reverse Code: 1111 1100 – 0000 0001 = 1111 1101
Original Code: 1111 1101 —> 1000 0010 —> -4 Decimal

The ANSI standard allows you to indicate that a constant is of type unsigned by suffixing u or U to the constant.

Change Data Sizes:
Sometimes, you want to reduce the memory taken by variables, or you need to increase the storage space of certain data types. C language gives you the flexiblity to modify size of data types.

The short modifier, a data type can be modified to take less memory by using the short modifier. For instance, you can apply the short modifier to an integer variable that is 32 bits long, which migh reduce the memory taken by the variable to as litter as 16 bits. By default, a short int data type is signed number:
eg: printf(“Hex of -12345 is 0x%X”, -12345); result is 0xFFFF CFCT
printf(“Hex of -12345 is 0x%hX”, -12345); result is 0xCFCT

Long modifier: more memory to keep values from a wide range.

You can add h into the integer format specifiet like this: %hd, %hu. Long is represented as “%L” or “%l”.

Have to include the header file math.h before you can use any math functions defined in the header file.

Pow syntax:
# include double pow(double x, double y);
x is raised to the power of y.

Sqrt syntax:
# include double sqrt(double x);
non-negative sqart root of x in double data type. The function returns error if x is negative.

Pow(x, 0.5) equals to Sqrt(x).

Note, all floating point calculations including both the float and double data type, are done in double-precision arithmetic. That is, a float data variable must be converted to a double in order to carry on the calculation. After the calculation, the double has to be converted to the float variable. Therefor a float calculation may take more time. THe main reason the C supports the float type is to save memory spaces, because the double data type takes twice as much memory space for storage as the float data type does.

分类: 科技 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
您必须在 登录 后才能发布评论.