C bit operations

Source: Internet
Author: User
Tags integer division logical operators

I. Concept of bit
We know that in a computer, one byte occupies 8 bits (some computers now have 16 bits), so the number range is 0-255, that is, 00000000-11111111. BITs are the numbers 0 and 1.
        CharC = 100;
    In fact, C should be 01100100, Which is exactly 64 h. Among them, the highest is in the front, and the lowest is in the back.
                 |     |
               7th bits 0th bits

Ii. bitwise logical operators

        Symbol        Description
         &          Bit logic and
         |          Bit logic or
         ^          Bit logic exclusive or
         ~          Invert
    Except that the last operator is a single object operator, the other operators are binary operators. These operators can only be used for integer expressions. Bit logical operators are usually used to set, clear, and reverse bitwise of integer variables, as well as to detect some selected bitwise. In a program, it is generally used by programmers as a toggle sign. Low-level hardware device drivers often require bitwise operations on input and output devices.

         &The calculation rule is that when both bits are 1, the result is 1; otherwise, the result is 0;
         |When both bits are 0, the result is 0. Otherwise, the result is 1;
         ^The calculation rule is that when the two phases are the same, the result is 0; otherwise, the result is 1;
         ~The calculation rule is that when the value is 1, the result is 0, and when the value is 0, the result is 1.

    Set bit: Set a bit to 1, while other BITs remain unchanged. Bit logic or operation can be used.
        CharC;
        C = C | 0x40;
    In this way, no matter how much C was originally, and 01000000 or later, 6th bits can always be set to 1, while other BITs remain unchanged.

    Clearing bits: Set one to 0, while the other BITs remain unchanged. Bit logic and operations are supported.
        C = C & 0xbf;
    In this way, the values of C and 10111111 and later can always set the value of 6th bits to 0, while those of other BITs remain unchanged.
    What if I want to set one to 1 and the other to 0?

Iii. Displacement Operators
         Symbol            Description
          <             Move left
          >             Right Shift

    The displacement operator acts on the variable on the left. The value of the expression on the right is the number of digits to be moved, and the calculation result is the result of the variable to be moved.
        B = A <2;
    That is, the value of a is shifted to the left and assigned to B. The value of a itself has not changed.
                
    To left shift is to add 0 at the low position, and to the right shift is to add 0 at the high position. When the right shift is performed, the result symbol bit can be maintained, that is, when the right shift is performed, if the highest bit is 1, it is a sign bit, then 1 is supplemented instead of 0.
    Programmers often perform integer division on the right shift operator and the Left shift operator on the integer multiplication operation. The factor used for multiplication and division must be the power of 2.

    For example, input an integer to determine the number of binary digits 1? For example, input 67, the output result should be 3. Because the binary number of 67 is 0000000001000011 (0043 H), three of which appear.
    Analysis: to determine whether it is 1, you only need to determine whether it is 1 or later. An integer that can be judged 16 times.

        Main ()
        {
            IntNum, K;
            IntCount = 0;              
            Scanf ("% d", & num );
            For (k = 0; k <16; k ++)
            {
                If (Num & 1 = 1)Count ++;   
                

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.