C Language Learning Note: 16_ bit arithmetic

Source: Internet
Author: User
Tags arithmetic bitwise bitwise operators

/* * 16_ bit arithmetic. c * * Created on:2015 July 6 * author:zhong */#include <stdio.h> #include <stdlib.h>/* bit operation: * The so-called bitwise operation refers to the operation with bits as the object. * The C language bitwise operators are: * & Bitwise AND * | Bitwise OR * ^ Bitwise XOR * ~ Bitwise negation * << left shift * >> right SHIFT * * except for ~ bitwise negation operator, all two mesh operators. Operands on both sides can only be integer or character data *----------------------------------------------------------------------------------------* A:& bitwise AND: Two corresponding bits are 1, then the result value is 1, otherwise 0; 0&0=0, 0&1=0; 1&0=0, 1&1=1 * Example: *00000111 (7) *&00000101 (5) * —————————————————— *00000101 (5) * therefore: 7&5=5. If a negative number is taken in the & operation, it is represented as a binary number in the form of 袚, then the bitwise AND ACTION * Note: The difference between && and &, && is the logical AND operator, the 7&&5 value is 1, because the non 0 is true, really true = 1; * * Bitwise AND ' & ' uses: * 1: Will be a number of ' clear 0 ' * As long as a binary number, the original number is 1 of the bit, the corresponding bit in the new number of 0 for the & operation, you can achieve the goal of the Qing 0. * Any number with 0 bitwise AND operation, the person is 0; Because two bits, as long as one is 0, it is 0; * 2: Take some of the points in a number to locate: * For example: Take the lowest byte in 3,004 bytes * 00000000 00000000 00000001 00101100 (300) * 00000000 00000000 00000000 11111111 ( 255) * ———————————————————————————————————————————————————————— *00000000 00000000 00000000 00101100 ($) *------------------------------------------------------------------------------------------* Two: | Bitwise OR: Two corresponding bits as long as one is 1, then this result value is 1; only two is 0 o'clock, only 0; 0&0=0, 0&1=1; 1&0=1, 1&1=1 *00000111 (7) *|00000101 (5) * —————————————————— *00000111 (7) *---------------------------------- ----------------------------------------------------------* Three: ' ^ ' xor operator: two bits that participate in the operation, the two-bit phase is false (0), the difference is true (1) that is 0^0=0 ; 0^1=1, 1^0=1, 1^1=0 * XOR means to determine whether two corresponding bit values are 0 ' xor ', for ' XOR ' on the True (1), otherwise false (0) *00000111 (7) *^00000101 (5) * —————————————————— *000000 10 (2) * Application: * 1, will binary you ' take anti ' *00111010 *^11111111 * ———————————————— * 11000101 * 2, and 0 ^, retain the original straight * * 3, Exchange two values, without temporary variable * a=a^ b B=b^a; A=a^b; In this way, the value of two variables can be exchanged *a=7,b=5;  *00000111 (7) *^00000101 (5) * —————————————————— *00000010 (2) a=a^b=2 *^00000101 (5) * —————————————————— *00000111 (7) B=a^b=7, (as B=b^a meaning) *^00000010 (2) * —————————————————— * 00000101 (5) a=b^a=5; (and a=a^b meaning is the same) * 4, a number and own XOR, the result is 0 * 5, two The number of different or sequential can be interchangeable, the result is the same. such as 9^5^9= 9^9^5 =0^5 = 5; * *----------------------------------------------------------------------------------------* Four: ~ is a single-mesh operator used to reverse the bitwise of a binary number, that is, 0 to 1, 1 Change 0 * ~00000111 (7) * —————————————————— * 11111000 (248) ~7=248 * *----------------------------------------------------- -----------------------------------* V: Left-shift Operation:<< is used to move a number of the bits all the left a number of bits, high-left shift overflow, shed. To the right 0 * For example A=15 will move a to the left 2 bits a=a<<2 = 60;                        * * * * * * * * ——————————————————————— * (00) 00 11 11 (00) (60) * &LT;&LT;2 Right 0 * * Left shift operation features: A number left shift 1 is equivalent to this number multiplied by 2, the left shift 2 bits is equivalent to this number multiplied by 2^2=4. (The precondition is that the high overflow bit can not be 1, can only be 0) * The left shift is faster than multiplication, so the compiler automatically will multiply the operation by 2 left one for the implementation, will be multiplied by 2^n to the left shift n bits. * Interview questions: Use the most efficient method to calculate 2 times 8 for a few *2<<3; Equivalent to 2*2^3=2*8 * *----------------------------------------------------------------------------------------* VI: Right SHIFT operation >> indicates that the bits is shifted to the right by the N-bit, and the lower end of the right side is discarded, the unsigned number, and the high 0. * For example: a=017; a=a>>2; * &GT;&GT;2 (+) * ——————————————————— * (00) 00 00 11 (11) (3) * Left-fill 0 this 2-bit Discard * Right shift operation characteristics: and left shift is just the opposite. A number right shifts 1 bits equal to this number divided by 2, right shift nBits are equivalent to dividing by 2^n times. * When moving right, be aware of the issue of the sign bit. For unsigned numbers, move right to the left high 0, and for signed data, if the original sign bit is 0 (positive), the left side is 0. * If the sign bit turns out to be 1 (negative), then the left shift is 0 or 1, depending on the computer system used. Some complement 0, some complement 1. * 0 is called "Logical right Shift" regardless of the symbolic problem. 1 is called "Arithmetic right shift" to keep the original symbol *----------------------------------------------------------------------------------------* Seven: Bitwise operator assignment: The combination of the Ascended and assigned operators: &=; |=; >>=; <<=; ^= * Function and *=, + = is the same. Like A&=b, the equivalent of a=a&b;. * * * If two data of different lengths are performed, the system will align the two to the right and the right side to 0; * *----------------------------------------------------------------------------------------*///bit arithmetic & ' and ' void  Bit_arith_and () {int a = 7, B = 5;printf ("7&5=%d\n", A & B),//output:5a = +, B = 255;printf ("300&255=%d\n", A & B); output:44}//bit Arithmetic | ' or ' void Bit_arith_or () {int a = 7, B = 5;printf ("7|5=%d\n", a | b);//output:7a = +, B = 255;printf ("300|255=%d\n", a | b); output:511}//bitwise operation ^ ' xor ' void Bit_arith_and_or () {int a = 7, B = 5;printf ("7^5=%d\n", a ^ b),//output:2}//4: Use ^ (XOR) algorithm, swap two The value of a variable void Use_or_swap_number () {int a = ten, b = 11;a = a ^ b;//1b = b ^ A;//11-1=10//b=a^b; A = a ^ B, as in the above sentence; 1+10=11printf ("Use ^ (XOR) algorithm to exchange two numbers: a=%d,b=%d\n", A, b);} Move left void Left_shift () {printf ("%d", 2 << 2),//2*2^2=8}//right shift void Right_shift () {printf ("%d", 8 >> 2);//8/2^2=2 ;} Use bitwise and & to determine whether a number is odd or even void Exercise_1 () {/** * odd and even characters: * 1111 (15) * 1001 (9) * 1110 (14) * 1010 (10) * * According to the above can be found: the right number of the most The latter one is 1, even the last one is 0; * Solving ideas: Just use this number &1 can take out the last one, if it is 1, is odd. Yes 0 is even * 1111 * 0001 (&1) * ———— * 0001 This takes out the last one * */int a = 15;int B = 20;a & 1 = = 1? printf ("%d is odd \ n", a): printf ("%d is even \ n", a); Using the three mesh operator B & 1 = = 1? printf ("%d is odd \ n", b): printf ("%d is even \ n", b); Use the three-mesh operator}//to output a decimal number in binary form with a bitwise operation void printf_bin (int numbers) {//int count = sizeof (number) *8-1;//Get the number of bits int count = ( sizeof (number) <<3)-1;   The number of digits to the left shift n is equal to the n-th square. <<3==*8; Because the arithmetic operator takes precedence over the bitwise operator, it is added () while (count >= 0) {int n_ = number >> count & 1;//First shift left 31 bit &1 get this digit ... printf ("%d", n_); if (count% 4 = = 0) {//four-bit empty one-cell: 0000 0000 0000 0000 0000 0000 0000 1111printf(" ");} count--;} printf ("\ n");} int main () {//bit_arith_and ();//bit_arith_or ();//bit_arith_and_or ();//use_or_swap_number ();//left_shift ();// Right_shift ();//exercise_1 ();p Rintf_bin (15); output:0000 0000 0000 0000 0000 0000 0000 1111return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C Language Learning Note: 16_ bit arithmetic

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.