In-process, memory analysis

Source: Internet
Author: User

One, the binary1, what is the binary

is a way of counting, a representation of a number

Kanji: 20 binary: 112 binary: 10,118 in: 13

Multiple binary: decimal, binary, octal, hex. In other words, we have at least 4 representations of the same integer

Software development, be sure to understand this

2. binary

1> features: only 0 and 1, every 2 into 1

2> writing format: 0b or 0b start

3> application: Binary instruction \ Binary, variable in memory is binary storage

4> Binary and Decimal conversions

5> N is the range of data that bits can represent (regardless of negative numbers): 0~2 N-square-1

3, eight binary

1> Features: 0~7, every eight into a

2> writing format: 0 opening

3> octal binary and binary conversion

4. Hex

1> Features: 0~f, every 16 into a

2> writing format: 0x or 0X start

3> 16 binary and binary conversion

5. Summary

1> use of calculators in Mac

2> printf output in a different binary form

Second, memory analysis of variables1, byte and address

To better understand the storage details of variables in memory, first recognize the "bytes" and "addresses" in memory.

1> memory in "bytes"

2> different types of bytes occupied are not the same

2. Storage of Variables

The number of bytes consumed by 1> is related to the type, and also to the compiler environment

2> Variable Instance

int B = 10;

int a = 134;

Memory from large to small addressing

Only binary forms are stored

Each variable has an address: the address of the first byte is the address of the variable.

3> two ways to view memory addresses:%x and%p

4> viewing the binary form of integers

1 //Binary form of the output integer2 voidPutbinary (intN)3 {4     intBITS =sizeof(N) *8;5      while(bits-->0) {6printf"%d", n>>bits&1);7         if(bits%4==0) printf (" ");8     }9printf"\ n");Ten}
3. Negative numbers stored in memory

1> the value range of a single byte

2> representation of negative numbers

3> original code, anti-code, complement

4. Range of Values

third, type specifier1. Short and long

The difference between 100l and 100LL and 100

The output of long and long long

Storage space occupied by different types

1> Short and long can provide integer numbers of different lengths, that is, the range of values that can be changed for integer numbers. In the 64bit compiler environment, int occupies 4 bytes (32bit), the value range is -231~231-1;short occupies 2 bytes (16bit), the value range is -215~215-1;long occupies 8 bytes (64bit), the value range is -263~ 263-1

2> summarizes: In a 64-bit compiler environment, short accounts for 2 bytes (16 bits), int accounts for 4 bytes (32 bits), and Long is 8 bytes (64 bits). Therefore, if you use an integer that is not very large, you can use short instead of int, which saves memory overhead.

3> in the world of compilers, different compiler environment, int, short, long, the range of values and occupy the length is not the same. For example, in a 16bit compiler environment, a long takes only 4 bytes. Fortunately, ANSI \ ISO has the following rules in place:

Short and int are at least 16 bits (2 bytes)

Long is at least 32 bits (4 bytes)

The length of short cannot be greater than the length of int,int.

Char must be 8 bits (1 bytes), after all, char is the smallest data type we can program

4> can use 2 consecutive long, that is, a long long. In general, the range of long long is not less than long, for example, in a 32bit compiler environment, a long long takes up 8 bytes, and a long occupies 4 bytes. However, in the 64bit compiler environment, a long long is the same as long, which occupies 8 bytes.

5> another point to make clear is that short int is equivalent to Short,long int equivalent to long,long long int equivalent to long long

2, signed and unsigned

1> first to be clear: Signed int is equivalent to signed,unsigned int equivalent to unsigned

The difference between 2> signed and unsigned is whether their highest bit is to be used as a sign bit, and does not change the length of the data, that is, the number of bytes, as short and long do.

Signed: Represents a symbol, which means that the highest bit is used as the sign bit, so it includes positive, negative, and 0. In fact, the highest bit of int is the sign bit, already includes the positive and negative number and 0, so signed and int are the same, signed equivalent to signed int, also equivalent to int. The value range of signed is 231 ~ 231-1

Unsigned: represents unsigned, which means that the highest bit is not used as a sign bit, so it does not include negative numbers. Under the 64bit compiler environment, int occupies 4 bytes (32bit), so the value range of unsigned is: 0000 0000 0000 0000 0000 0000 0000 0000 ~ 1111 1111 1111 1111 1111 1111 11 11 1111, i.e. 0 ~ 232-1

four-bit arithmetic1. & Bitwise AND

1> function

Only the corresponding two binaries are 1 o'clock, the result bit is 1, otherwise 0.

2> Example: 9&5, in fact, is 1001&101=1, so 9&5=1

3> Law

Binary, with 1-phase & to remain in place, and 0-phase & is 0

2, | bitwise OR

1> function

As long as the corresponding two binary has one for 1 o'clock, the result bit is 1, otherwise 0.

2> Example: 9|5, in fact, is 1001|101=1101, so 9|5=13

3, ^ bitwise XOR or

1> function

When the corresponding binary is different (not the same), the result is 1, otherwise 0.

2> Example: 9^5, in fact, is 1001^101=1100, so 9^5=12

3> Law

The result of the same integer ^ is 0. Like 5^5=0.

The results of multiple integers ^ are independent of the order. Like 5^6^7=5^7^6.

Thus concludes: A^b^a = b

4, ~ Take the reverse

The binary of integer A is reversed, and the sign bit is reversed (0 to 0)

5. << left Shift

All the binary of the integer A is left n bits, the high is discarded, and the low is 0. The n-bit left shift is actually multiplied by 2 of the n-th square

Since the left shift is the highest bit discarded, 0 is the lowest bit, so the sign bit will also be discarded, the result value of left shift may change the positive and negative

6. >> Right Shift

Shift all the binary in integer A to the right n bits, keeping the sign bit constant. The right shift n is actually divided by 2 of the n-th square

is positive, the sign bit is 0 and the highest bit is 0

is negative, the sign bit is 1, the highest bit is 0 or 1 depends on the rules of the compiling system

v. Type of char1. Storage Details

ASCII single-byte table (double-byte gbk\gb2312\gb18030\unicode)

2. Common Mistakes

char C = A;

char c = "A";

char c = ' ABCD ';

char c = ' male ';

3. Use as Integral type

In the -128~127 range, it can be used as an integer.

4. Use of%c and%d\%i

printf ("%d", ' A ');

printf ("%c", 68);

5. Escape character

Escape character

Significance

ASCII Code Value

\ n

Moves the current position to the beginning of the next line (carriage return line)

10

\ t

Jumps to the next tab position

9

\\

Represents a backslash character

92

\‘

Represents a single quote character

39

\"

Represents a double-quote character

34

/

Null character

0

In-process, memory analysis

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.