Dark Horse programmer--c Language Basics---Binary memory parsing and type specifiers

Source: Internet
Author: User

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

One, the binary 1. What is a binary

L is a way of counting, a representation of a number

Kanji: 20 binary: One -by-one binary:1011 octal:

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

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.16 Binary

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

2> writing format:0x or 0X start

3> 16 binary and binary conversion

Second, memory analysis of variables

Study the specific storage of variables in memory

1. 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;

L Memory from large to small addressing

L Only store binary form

L 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

Binary form of the output integer

void putbinary (int n)

{

int bits = sizeof (n) * 8;

while (bits-->0) {

printf ("%d", n>>bits&1);

if (bits%4==0) printf ("");

}

printf ("\ n");

}

3. Negative numbers in-memory storage

1> the value range of a single byte

2> representation of negative numbers

3> original code, anti-code, complement

Three, type specifier 1. Shortand theLong

L 100l and 100LL and the difference between

L Long and long long 's output

L storage space occupied by different types

1> Shortand theLongyou can provide an integer number of different lengths, that is, you can change the range of values of the integer number. In the64bitcompiler environment,intoccupy4bytes (32bit), the value range is-231~231-1; Shortoccupy2bytes (16bit), the value range is-215~215-1;Longoccupy8bytes (64bit), the value range is-263~263-1

2>to summarize: in --bit compiler environment, Shortaccounted for2of bytes(bit),intaccounted for4of bytes( +bit),Longaccounted for8of bytes( +bit). Therefore, if you use an integer that is not very large, you can use the ShortReplaceint, so that you save more memory overhead.

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

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

L Long is at least 32 bits (4 bytes)

L short length cannot be greater than int,int length cannot be greater than long

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

4>can be used continuously2aLong, namelyLong Long. In general,Long Longthe range is no less thanLong, such as in32bitcompiler environment,Long Longoccupy8bytes,Longoccupy4a byte. But in64bitcompiler environment,Long LongwithLongis the same, all Occupy8a byte.

5> Another point to make clear isthat shortint is equivalent to Short, andlong int is equivalent to long,longlong int equivalent to long LonG

2. Signedand theunsigned

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

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

L Signed: Indicates 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

L unsigned: denotes unsigned, that is, the highest bit is not used as the symbol 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, bitwise operation 1. & 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

L binary, with 1 phase & to remain in place, and 0 phase & 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

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

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

L therefore concluded that a^b^a = b

4. ~Take counter

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

5. <<Move left

L Shift all the binary of integer A to the left n bit, high discard, low 0. The n-bit left shift is actually multiplied by 2 of the n-th square

L Because left shift is discard the highest bit, 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 character

6. >>Move Right

L 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

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

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

Five, Chartype1. Storage Details

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

2. Common errors

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.

Dark Horse programmer--c Language Basics---Binary memory parsing and type specifiers

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.