Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the concept of source code, inverse code, complement and bit operation in C language?

Shulou Source: shulou.com Published: 2022-06-01 04:02:45 09月30日 Update

In this article, the editor introduces in detail "what is the concept of C language source code, inverse code, complement and bit operation". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "what is the concept of C language source code, inverse code, complement and bit operation" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.

I. original code, inverse code and complement code

The computer only has the addition calculator, and the data stored and calculated in the calculator are all complement codes. The original code, inverse code and complement code of positive number and 0 are the same, but the original code, inverse code and complement code of negative number are different.

Original code: symbol bit + absolute value (0 for positive, 1 for negative)

Inverse code: the symbol bit is unchanged, and the other bits are inverted.

Complement: inverse + 1

1. Why use complement storage and complement calculation?

Because the calculation of source code and inverse code will have the problem of + 0 and-0 and calculation error, while the complement is correct and simple, and the symbol bit is also directly involved in the operation.

Example:

Int is 4 bytes and 1 byte is 8 bits, so an int value is 32 bits, and the first bit is a symbol bit, so the value range of int is-2 ^ 31 ~ 2 ^ 31-1 (10000000 00000000 00000000 00000011 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

Calculate 2 + (- 1) and 1 + (- 1) respectively, the process is as follows:

The original code, inverse code and complement code of 2 are 00000000 00000000 00000010 and 0000000000000010 respectively

The original code, inverse code and complement code of 1 are 00000000 00000000 00000001pr 0000000000000001pr 0000000000000001 respectively.

The original code, inverse code and complement code of-1 are 10000000 00000000 00000001, respectively. 11111111111111111111111111111111101011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

2 and-1 source code is added, the result is: 10000000 00000000 00000011 (original code), 0 represents positive, 1 represents negative, so the value is-3, error.

When the inverse codes of 2 and-1 are added, the result is 00000000 00000000 00000000 (inverse code), the result of the corresponding original code is 00000000 0000000000000000 (original code), the value is 0, error.

2 and-1 complement are added, the result is: 00000000 00000000 00000001 (complement), the result of the corresponding original code is 00000000 0000000000000001 (original code), the value is 1, correct.

1 and-1 source code is added, the result is: 10000000 00000000 00000010 (original code), the value is-2, error.

1 and-1 inverse codes are added, the result is 11111111 11111111 11111111 (inverse code), the corresponding result of the original code is 10000000000000000000000 (original code), the value is-0, which is not accurate. (for the design of + 0 and-0, you can do it yourself if you are interested.)

1 and-1 complement are added, the result is: 00000000 00000000 00000000 (complement), the result of the corresponding original code is 00000000 0000000000000000 (original code), the value is 0, correct.

2. Bit operation

Bit operators include: and (&), or (|), non (~), XOR (^), move left (), move unsigned right (>)

If the &: binary bit is 1 at the same time, the result is 1, otherwise it is 0

| |: one of the bits is 1, and the result is 1, otherwise it is 0 |

~: bit 0 to 1 pm 1 to 0

^: the bit is different, the result is 1, otherwise it is 0

The bit moves to the right as a whole, with 0 on the left side of the positive number and 1 on the left side of the negative number

>

Example:

Public class BitOperationTest {public static void main (String [] args) {int a = 13, b = 6; System.out.println ("a:" + getBinaryStr (a)); System.out.println ("b:" + getBinaryStr (b)); System.out.println ("aquib:" + getBinaryStr (aqb)); System.out.println ("a | b:" + getBinaryStr (a | b) System.out.println ("~ a:" + getBinaryStr (~ a)); System.out.println ("a ^ b:" + getBinaryStr (a ^ b)); System.out.println ("ab:" + getBinaryStr (a > > (b-4)); System.out.println ("- a:" + getBinaryStr (- a)); System.out.println ("a > b:" + getBinaryStr (a > > (b-4) System.out.println ("- a > b:" + getBinaryStr ((- a) > > (b-4);} private static String getBinaryStr (int n) {StringBuilder str = new StringBuilder (Integer.toBinaryString (n)); int len = str.length (); if (len)

< 32) { for (int i = 0; i < 32 - len; i++) { str.insert(0, "0"); } } return str.substring(0, 8) + " " + str.substring(8, 16) + " " + str.substring(16, 24) + " " + str.substring(24, 32); }}//Result a :00000000 00000000 00000000 00001101 b :00000000 00000000 00000000 00000110 a&b :00000000 00000000 00000000 00000100 a|b :00000000 00000000 00000000 00001111 ~a :11111111 11111111 11111111 11110010 a^b :00000000 00000000 00000000 00001011 a2 :00000000 00000000 00000000 00000011(-a)>

> 2: 11111111 11111111 11111111 11111100 a > > 2: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Tags: Complement inverse code result operation symbol positive number negative number error concept language whole article mobile binary representative content at the same time bytes examples storage Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker NVidia OPPO Reno MySQL