The computer storage form and the binary conversion

Source: Internet
Author: User
Tags bitwise

Because the computer can recognize a binary sequence, the computer stores a number in binary form. For a positive number, store it in the form of the original code. For negative numbers, it is stored in the form of complement.


The original code of the positive number, the inverse code, the same complement:

Example: 1

Original code: 00000000 00000000 00000000 00000001

Reverse code: 00000000 00000000 00000000 00000001

Complement: 00000000 00000000 00000000 00000001


Negative numbers: Negative numbers are stored in the form of complement (the highest bit is the sign bit, 1 is negative, 0 is positive)

Example:-1

Original code: 10000000 00000000 00000000 00000001

Anti-code: 11111111 11111111 11111111 11111110 (the sign bit is unchanged, the remaining bits are reversed)

Complement: 11111111 11111111 11111111 11111111 (original code inversion plus one)


Small end: because the smallest unit of computer storage is bytes, and one byte is 8 bit bits, we represent the integer 4 bytes of 1 as: XX

Example: 1

Big- endian form: XX

Small End form: XX

Usually we write with a big-endian form, and the computer is stored with small-end storage, we can verify:

union b{                  int a;                  char b;}; Int main () {                  union B A;                 A.a = 1;                 printf (  "%c\n", a.b);                 system (  "pause");                 return 0;} 

In the form of small-end storage, the output should be a value of ASCII code 1.


Binary and Decimal conversions:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7E/79/wKioL1cB-pKjFrThAABYDh-Ui3U144.png "title=" Untitled. png "alt=" Wkiol1cb-pkjfrthaabydh-ui3u144.png "/>


650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

>> Right Shift: binary right shift, positive words empty out of the complement 0;

Example: 7>>2=1 (note can not be written as 7<<-2, here is the wrong form)

00000000 00000000 00000000 00000111

00000000 00000000 00000000 00000001

<< left Shift: empty bit complement 0

Example:4<<1=8;

00000000 00000000 00000000 00000100

00000000 00000000 00000000 00001000


Arithmetic shift and logical shift:

There is only one case to consider when the negative is shifted right: because the highest bit of the negative number is the sign bit, when you move to the right, the vacated bit is 0 or 1.

Arithmetic shift: an empty bit complement 1.

Logical shift: vacated bit complement 0.

Whether the arithmetic shift or the logical shift, depends on the computer platform, so when the negative number of the right shift, its program is not portable.


Bitwise AND: For each of the binary operations, the same as 1 is 1, otherwise 0;

Example 7&5=5

00000000 00000000 00000000 00000111

00000000 00000000 00000000 00000101

00000000 00000000 00000000 00000101


Bitwise OR: For each operation of the binary, at least one bit is 1 for 1, otherwise 0;

Example: 8|4=12

00000000 00000000 00000000 00001000

00000000 00000000 00000000 00000100

00000000 00000000 00000000 00001100


XOR: For each of the binary operations performed, the same is 0, the difference is 1;

Example: 7^4=3

00000000 00000000 00000000 00000111

00000000 00000000 00000000 00000100

00000000 00000000 00000000 00000011


Negation: For each operation of the binary, 1 change 0,0 to 1;

00000000 00000000 00011000 00000011

11111111 11111111 11100111 11111100





This article is from the "11132019" blog, please be sure to keep this source http://11142019.blog.51cto.com/11132019/1760091

The computer storage form and the binary conversion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.