Dark Horse Programmer-the memory Anatomy of C language Foundation

Source: Internet
Author: User
Tags bitwise

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

In learning the basic knowledge of C, the teacher attaches great importance to the analysis of memory, almost every knowledge point will help us to analyze memory storage situation, through the memory analysis also let us have a deeper understanding of C language, C language and other computer languages, the memory of the analysis and requirements of higher, Below we together to understand and learn the C language memory analysis, is my personal in the study of some summary, if the problem or not in place also hope that we can communicate together and correct.

First-speaking system

The binary is a way of counting, the representation of numeric value, generally our C language mainly study includes decimal, binary, octal, hex. In other words, we have at least 4 representations of the same integer. Before the C program run, we all know to compile and link first, and the data in the computer expression is 0 and 1, the compilation is to translate the program code into the computer can read the process of 0 and 1. an int type data occupies four bytes, 32bit, for example 0000 0000 0000 0000 0000 0000 0000 1100, variable storage details in memory problem, memory addressing from large to small, variable allocation byte is also from large to small, And the address of the variable is the smallest byte address it occupies.

1. Binary

Features: only 0 and 1, every 2 into 1

Writing format: 0b or 0B start

Use cases: binary or binary, variables are binary stored in memory

Binary and Decimal conversions: for example: 0b1100=0*2 0 Square +0*2 of 1 square +1*2 of the 2 square +1*2 of the 3 square, the decimal binary system is in addition to 2

The range of data that n-bit bits can represent (regardless of negative numbers): 0~2 N-1

2. Eight binary

Features: 0~7, every eight into a

Writing format: 0 start

Cross-conversion between octal and binary

3.16 Binary

Features: 0~f, every 16 into one

Writing format: 0x or 0X start

Reciprocal conversions between hexadecimal and binary

4. Summary of some common format characters

%d or%i outputs a numeric value in the form of a signed decimal integer

%o output integers in unsigned octal form

%x outputs integers in unsigned 16-binary form

%c outputs one character,%s outputs one or more characters

%f output single, double precision number, default output six decimal places,%p output address

5. Type specifier

int 4 bytes%d

Short 2 byte%d

A long 8 bytes%ld

A long long 8 bytes%lld

The difference between signed and unsigned:

Signed the highest bit to be symbolic, positive, 0, negative is actually equivalent to int

Unsigned highest bit not as symbol bit unsigned, positive, 0

Second speaking bit operation

I have also learned a lot about the basic arithmetic of C language, including arithmetic operation, assignment operation, self-increment decrement operation, relational operation, logic operation and three mesh operation, and so on, and the bit operation here is inseparable from the representation of data in computer memory. Because the data exists in the computer in the form of 0 and 1, the bitwise operation is also based on this formal operation.

1. Bitwise AND &: only the corresponding two binaries are 1 o'clock, the result bit is 1, otherwise 0

like Binary 1001,5 binary of 9 is 0101,,9&5=1

Rule: binary, with 1-phase & to remain in place, and 0-phase & 0

2. Bitwise OR |: as long as the corresponding two bits have one for 1 o'clock, the result is 1, otherwise 0

3. Bitwise XOR or ^: when the corresponding two bits are different (different), the result is 1, otherwise it is 0

Rule: The same value is different or the result must be 0, such as 9^9==0

Swap: 9^5^6==9^6^5

Any value is different from 0, and the result is the original value, 9^0==9

Can be launched A^b^a==b

There is a small example of what can be contacted before: (omitted #include<stdio.h> and main function)

1 //Exchange The value of two variables without introducing a third variable2     intA=Ten;3     intb= One;4     5a=a^b;6b=a^b;7a=a^b;8printf"a=%d,b=%d\n", A, b);//a^b^a==b can be obtained by using the characteristics of different or

4. Bitwise counter ~: Binary 0 Change to 0, sign bit will also be reversed

5. Shift left <<: all bits of the whole number a left n bits, high drop, low 0, left n is multiplied by 2 of the n-th square, a<<n

9<<1->9*2 1-Time Square ==18

9<<2->9*2 2-time = = 36

9<<n->9*2 N-Th square

6. Shift Right >>: bits all the integers a to the right n bits, keep the sign bit constant, the right shift n is actually divided by 2 of the N-square, A>>n

8>>n->8 divided by 2 n-Th square

These are some of the knowledge about the binary and bit operations, which may be a bit stiff and boring, let's look at an exercise in bitwise arithmetic to get a deeper look at some of the memory representations:

1 //write a function to output the binary form of an integer in memory2#include <stdio.h>3 4 voidPrintbinary (intnumber);//make a declaration of a function5 6 intMain ()7 {8     9Printbinary ( the);//Call this function and assign a value ofTen     return 0; One      A } -  - voidPrintbinary (intNumber ) the { -     intTemp= (sizeof(number) <<3) -1;//The record now moves to the first few, change to sizeof (number) *8-1==31, actually still -      -      while(Temp >=0) { +          -         intValue= Number>>temp &1;//move right to each bit with up to 1 to get this number of digits +          Aprintf"%d", value); atTemp--; -          -         if((temp+1)%4==0) {//every four bits separated by a space -printf" "); -         } -          in     } -printf"\ n"); to  +}

Summary: Maybe this knowledge point is a bit dull and blunt, but C language memory analysis is very important, learned to analyze memory on the future array pointers and other knowledge of learning is very helpful, so still need to consolidate this piece of basic knowledge of learning.

Dark Horse Programmer-the memory Anatomy of C language Foundation

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.