[Xcode to learn C-4] hexadecimal knowledge, bit operators, variable storage details and pointer knowledge Introduction

Source: Internet
Author: User
Tags bitwise operators

I. hexadecimal knowledge


(1) The default value is decimal. Add 0 to the front of the octal node, that is, int num1 = 015; it is 13. Add 0x/0x before the hexadecimal notation, that is, int num1 = 0xd. The result is 13. The first part of the binary is 0b/0b, that is, int num1 = 0b1101, and the result is 13.


(2) the output can be octal, decimal, and hexadecimal, namely % O, % d, and % x, but no binary format is used for direct output.


(3) The three-bit binary is 0 ~ 7. Three binary bits are equivalent to octal data. For example, 000001101 (Binary 13) is divided into three groups of 000 001 101, which are then converted into decimal values: 0 1 5, and the 015 value is the octal value 13.


(4) Same as above, binary conversion to hexadecimal is a set of four groups before conversion.


(5) re-fill the source code below the brain to complement the reverse code. All the data stored in the computer is a supplemental code. Three-digit combination of positive numbers. Three negative numbers have passed each other. For example, if the value is-13, the original code is 1000 1101 (the highest bit is 1, indicating a negative number, and the value is 0, indicating a positive number). The reverse code is 1111 if all the other values remain unchanged except the highest bit, the complement Code is based on the reverse code plus 1, that is, 0000 0001, so the complement code is 1111 0011.


Ii. bitwise operators


(1) & bitwise AND, only two values are 1 and 1 is returned, that is, 9 & 5 = 1. Because 1001 & 0101 = 0001, 1 is returned. Therefore, if any number is equal to 1, the result is still the number.


(2) Likewise, | by bit or. If one is 1, 1 is returned.


(3) returns 0 if it is an exclusive or ^, that is, 0 if it is both 1 or 0, and 1 if it is different. When multiple integers are bitwise OR, the order does not affect the final result. The result is 0 because the same integer is bitwise OR the result is 0. If any integer is equal to or greater than 0 by bit, the result is still the integer.


(4) reverse retrieval ~, It is to change 1 to 0 to 1.


(5) Left shift <, 9 <1 means to shift the binary bits of 9 to the left. The number of digits to move to the left is equal to 9*2 ^ (number of digits), that is, the result of moving 1 to the left is 9*2, and the result of moving 2 to the left is 9*4 ....... However, the Left shift may change the positive and negative values, because the high position is discarded and the low position is filled with 0. Usage: When you need to calculate the power of a value * 2.


(6) Likewise, the right shift is divided by the power of 2.


Iii. Variable storage details


(1) The Minimum Memory Unit is byte. If an int value requires 4 bytes, the value of the memory allocation address will be allocated from large to small, place the highest bits of a value in a large address, and place the values one by one in a small address. And use the final smallest address value as the reference address of the entire value.


(2) Change the value range in one of the type modifiers. Int Is 4 bytes, short and long are 2 and 8 bytes. If the int value cannot fit the numeric value, you can use long Int = 1234567898765l to write it. This statement means to forcibly change the number of int bytes to 8 bytes, so that you can store larger data. Generally, we add L or lower-case l after the corresponding value. The corresponding formatted output is % lD. If it is short, it is % HD. If it is long, it is % LLD.


(3) symbol of Type modifier 2. The default value is signed, which is positive, negative, and 0. The value range is-2 ^ (31 )~ 2 ^ (31 ). Unsigned can only assign values to positive numbers and 0. The value range is 0 ~ 2 ^ (32 ). It can be used in combination with the above modifier.


(4) When an array is used as a form parameter, no specific number of elements can be written, for example, arr []. The storage sequence of the array is opposite to that of the variable. The storage sequence is from small address to large address.


(5) two-dimensional array. Int ages [2] [3] = {1, 2, 3}, {4, 5, 6 }};


(6) A string is essentially similar to a character array, but the last character in the string storage has a \ 0 character, and the read ends when \ 0 is encountered. Strcat () concatenates two strings. strcpy (A, B, 1) Copies B to A and only copies 1 character. Strcmp (A, B, 2) compares two strings, returns 0 if they are equal, returns a positive number if the preceding value is greater than the following value, and returns a negative number if the preceding value is greater than the following value. The upgraded version is strncmp (A, B, 2). 2 indicates that only the first two characters are compared.


(7) A string array is equivalent to a two-dimensional array. Char A [2] [10] = {"hhhhhh", "kkkkkk "};.


Iv. pointer


(1) pointer variables are identified by *, for example, int A = 5; int * P = & A; P pointer stores an address of, directly use * P = 20; * P is equivalent to accessing the memory in the P address and changing the value to 20, that is, the value of a is 20.

// Pointers are actually transmitted between addresses. The P pointer itself is the address variable, and * P directly accesses the memory pointed to by this address. // Clear the pointer with = nullvoid change (int * P) {* P = 80;} int main (INT argc, const char * argv []) {int num1 = 90; change (& num1); printf ("% d \ n", num1); Return 0 ;}

(2) ** P indicates the pointer to the pointer. If you want to access the value, it is ** p.


(3) the pointer variable occupies 8 bytes. Why define the data type int for the pointer variable. It only tells the pointer how many bytes are taken when the value is set.


(4) pointers and arrays. If int * P = & arr [0]; then * P is the first value of the array, * (p + 1) is the second value, and so on. Here + 1, is the number of bytes occupied by the pointer type variable.


(5) array name = array address, that is, arr = & arr [0];.


(6) define a pointer to the function: Call (* p. However, we generally use P (); To call it directly.

Void change () {printf ("jjj");} int main (INT argc, const char * argv []) {// The following two rows are, it is equivalent to changing the function name to (* P), and then assigning a value to void (* p) (); P = change; return 0 ;}

(7) The function can be passed as a parameter, which is called a callback function in Java.

[Xcode to learn C-4] hexadecimal knowledge, bit operators, variable storage details and pointer knowledge Introduction

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.