09-c language input, memory analysis

Source: Internet
Author: User
Tags bitwise integer numbers

One, the binary 1. What is a binary

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

Count the number of squares

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

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

L software development, definitely want 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-1 3. Octal

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

2> writing format: 0 opening

3> octal and binary conversions to each other 4. Hexadecimal

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

2> writing format: 0x or 0X start

3> 16 binary and binary conversion to each other 5. Summarize

1> use of calculators in Mac

2> printf output in a different binary form

6. Exercises

1> judge if the following figures are reasonable

00011 0x0011 0x7h4 10.98 0986.089-109

+178 0b325 0b0010 0xFFdc 96f 96.0f 96.0F

-.003 15.4e6 10e8.7 7.6e-6

2> write their decimal, octal, hexadecimal, respectively

0b0011 1101 0b0111 1011

3> write their binary

056 0x004f Memory Analysis of variables

Study the specific storage situation of variables in memory 1. Bytes and Addresses

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

1> memory in "bytes"

2> the bytes occupied by different types are not the same as 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 are 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

5. Exercises

Write the following variables in memory storage

int a = 134;

int b = 0;

int c =-10; Three, type specifier 1. Short and long

L 100l and 100LL and 100 difference

L Long and long Long's output

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

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 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 the short int is equivalent to the Short,long int equivalent to the long,long long int equivalent to a 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.

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, that is, 0 ~ 232-1 Four, bit 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 & 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

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. << left Shift

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 the left shift is the highest bit discarded, 0 is the lowest bit, so the sign bit will also be discarded, the result value left out may change the positive and negative 6. >> Right Shift

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

When L is negative, the sign bit is 1, the highest bit is 0 or 1 depends on the requirements of the compiling System 7. Exercises

1> using the bitwise XOR operator to interchange the values of two variables without introducing other variables

2> parity between bits and & operator variables

3> writes a function to output integers in memory in binary form five, char type 1. 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

Within the -128~127 range, you can use 4 as an integer. 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 6. Exercises

1. Write a function to capitalize the lowercase letters

2. Say the output of the program

int main ()

{

int i = 67 + ' 4 ';

char c = ' C '-10;

printf ("%d-%c\n", I, I);

printf ("%d-%c\n", C, c);

return 0;

}

09-c language input, memory analysis

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.