C Language: binary and bitwise operations

Source: Internet
Author: User

I. In-processdecimal-to-n-binary : Even removing the rewind remainder.

In 10, for example, different binary representations:

Decimal: 10;

Binary: 0b1010;

Octal: 010;

Hex: 0x10;

int a = 100;

printf ("%o", a);

/*

How to output the binary number:

%d------Decimal

%o------Octal

%0x-----Hex

*/

/*

Bitwise operators: Bitwise AND &, bitwise OR |, bitwise NON ~, bitwise XOR, Shift left <<, right Shift >>

Bitwise AND &: The same 1 is 1, otherwise 0, often used for a certain one Qing 0.

Bitwise OR | : The same 0 is 0, otherwise 1. Often used to keep one,

Bitwise NON ~: If it is a signed number, the highest bit represents the sign bit, 1 for negative, and 0 for positive.

When the data is stored in memory, it is stored in the form of complement, the complement of the positive number is itself, the complement of the negative is the absolute value of the inverse plus 1.

Bitwise XOR ^: The same is 0, the difference is 1;

*/

Shift left <<:

unsigned char d = 1;

printf ("%d", D << 4);

Shift Right >>

unsigned char a = 255;

printf ("%d", a >> 1);

unsigned char number = 0b01100100;

unsigned char left = number << 4;

unsigned char right = number >> 4;

unsigned char result = left | Right

printf ("%d", result);

/*

Swap 10010010 odd and even bits

unsigned char num = 0b10010010;

The 0 operation uses the bitwise &, the number of reserved bits is 1, the clear 0 digit is 0;

1. Change odd digits to even digits and move left one

unsigned char left = num << 1;

2. Reserved even digits, odd digit 0

unsigned char Clearji = left & 10101010;

3. Turn the even digits into odd digits and move right One

unsigned char right = num >> 1;

4. Reserved odd digit even number of bits clear 0

unsigned char Clearou = right & 01010101;

5. Last Bitwise OR |

unsigned char result = Clearji | Clearou;

printf ("%d", result);

*/

/*

Range of values for data types:

Unsigned: Char type 0--255 (2 of 8-square-1);

Short 0--2 of 16 times-1;

int 0--2 of 32 times-1;

Signed: Char-2 7 times-2 of 7 square-1;

Short-2 15-2 of the 15-square-1;

Int-2 31-2 of the 31-square-1;

*/

Char B = ~ ~;

int a = 5 ^ 7;

printf ("%d", b);

Stack memory allocation principle: from high to low allocation, from low to high access.

int a =-5;

printf ("%p\n", &a);

int B = 10;

printf ("%p\n", &b);

The array name represents the first address of the array, which is the address of the first element of the array, which is a constant address

int a[4] = {1, 2, 3, 4};

printf ("%d\n", &a[0]);

printf ("%p\n", &a[1]);

printf ("%p\n", &a[2]);

printf ("%p\n", &a[3]);

printf ("%p\n", &a);

Exchange two number of values without third-party variables

int a = 20;//1001

int B = 36;//0101

printf ("before exchange a =%d,b =%d\n", A, b);

A = a ^ b;//1100

b = b ^ a;//1001

A = a ^ b;//0101

printf ("After exchange a =%d,b =%d\n", A, b);

C Language: binary and bitwise operations

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.