Left and right operating system conversion distinguishing _

main(  ){
   char  c= 040 ;
   printf(“%o \n",c<< 1 );
}
A left equals 2 multiplied by 080 to obtain because octal, the output 0100 into a binary output according to 8, the output 100;
Computer distinguished: ordinary number, decimal, preceded 0X hexadecimal, preceded by 0 is octal.
Octal numeral 0-7 composed by the expression preceded by the number 0
 
Bitwise Operators:
1, &: Bit Logic and
2, |: Bit logic or
3, ^: bitwise logical XOR
4, ~: Anti-bit logic
5, >>: right
6, <<: Left
 
Bit operation is a byte or word is detected actual position, or shift set, it applies only to character and integer variables and variants thereof, is not applicable to other types of data.
(1) logical AND operation & rules: when a logical AND operation of two numbers, the first two into a binary number, then a logical AND operation, when the same number of bits is only when both 1, the result was 1, and 0 otherwise.
a=5;
b=4;
c=a%b;
c=4;--------------  a=5=0101  b=4=0100  a&b=0100=4
(2) | logical or arithmetic rule: when a logical OR operation of two numbers, the first two into a binary number, and a logical OR operation, when the same number of bits as long as one of them is 1, the result 1, and 0 otherwise.
a=2;b=9;
c=a|b;  a=2=0010; b=9=1001  c=1011=11
(3) the exclusive OR operation rules: when the logical exclusive OR operation of two numbers, the first two into a binary number, and logical exclusive OR operation, when the same number of bits only when both are different digital, the result was 1, and 0 otherwise.
a=6;b=8; c=a^b  ;  a=0110  b=1000; 0110^1000=1110=14;
(4) anti-arithmetic logic rule: when a logical inverse operation number, the first number into a binary, logical inverse operation then, when the number is 1, the result is 0, the result is 0 when the number of 1.
a=12;b=~a:  12=1100;~1100=0011=3

According to the above calculation rules, we first converted to binary 12, 1100, anti-bit-logic operations, compared with 0011, the result should be a decimal number into 3 fishes ah, why the test results -13 it?
The original ah, in the computer, storage is a binary code stored in the form of a supplement.
Therefore, stored in the computer 12 is 01100 (0 a first sign bit, representing the number is positive), subjected to inverse logical operation was 10,011, but this is only stored in the form of a binary variable b in the computer ( i.e. complement form b), so we need to convert the original code form, since the sign bit is 1, is negative, so we first converted to the inverted 10011, i.e., 11100, and then subjected to +1, so the result 11101 is converted to a decimal number, compared with -13. Therefore, b = -13.

Binary computer code print are complementary forms of

Positive original code, anti-code and complement three yards one 

Negative original code is converted into complement sign bit constant value of the last one bit bitwise plus a

Negative, complement of the complement of the original code is equal to  the inverted positive three yards one

 

(5) right shift operation rules: the left to fill the vacated bit with 0 or 1. 0 filled with positive, negative, filled with 1. Note: The way to fill the different environments may be different; the lower right overflow This bit is discarded.

a = 127, converted to binary to 01111111, since in binary is stored in the form of a computer inside the complement, but since 127 is positive, so its complement is 01111111, the complement arithmetic right shift operation, in accordance with the operation rules to give 00011111, is converted to the original code, still as 00011111, is converted to the decimal number 31, therefore, b = 31.

Multiple sets of test data, we can get such a law:
When the number of new right shift operation performed a number of n bits, the resulting operand is = / 2 ^ n.
(6) left arithmetic operation rules: the right vacated bit positions with zeros to fill, the upper left upper overflow is discarded.

a = -127, is converted to binary 100001111111, is converted to the inverted 111,110,000,000, is converted into complement 111,110,000,001, three left shifts its operation to obtain: 110000001000, to convert is inverted: 110000000111, which is then converted into the original code: 101111111000, converted to decimal numbers, as -1016, so b = -1016.
A plurality of sets of test data, we can get a pattern:
new number when a number of n-bit left shift operation performed, the resulting operand is = * 2 ^ n.

Guess you like

Origin www.cnblogs.com/samanian/p/11391894.html