A brief analysis of C + + shift operation and binary conversion

Source: Internet
Author: User

The shift operations include "logical Shift" (logical shift) and "arithmetic Shift" (arithmetic shift).

Logical shift: bit discarded, vacant bit (vacant bit) filled with 0.

Arithmetic shift: The bit discarded, the vacant bit (vacant bit) is filled with "sign bit", so it is generally used in the right-shift operation.

In C + +, integers are divided into two symbols and unsigned numbers. For these two numbers do the left shift and right shift operations. Slightly different:

1, assume that the unsigned number, whether left or right shift is a "logical shift." example, the result of left shift and right shift operation for unsigned number 179 is the complement 0 operation;

2 . Assume that there are signed numbers. To move left, the "logical shift" is the same as the left shift of the unsigned number. Assuming that you do a right-shift operation, the "arithmetic shift" is done.


Finally, this paper gives the recursive working mechanism legend of decimal number conversion to binary. For example, the following


The 10 binary number is converted to a binary number, which is a continuous process in addition to 2:

Divide the number to be converted by 2 to get the quotient and remainder.

Continue dividing the quotient by 2 until the quotient is 0. Finally, the whole remainder is arranged in reverse order, and the number is the conversion result.



Decimal to hexadecimal source code

 #include <iostream> #include <string>using namespace std;string str_16= "";        string switch_10_16 (int ch) {switch (ch) {case 0:return "0";        Case 1:return "1";        Case 2:return "2";        Case 3:return "3";        Case 4:return "4";        Case 5:return "5";        Case 6:return "6";        Case 7:return "7";        Case 8:return "8";        Case 9:return "9";        Case 10:return "A";        Case 11:return "B";        Case 12:return "C";        Case 13:return "D";        Case 14:return "E";    Case 15:return "F";         }}long int change (long int num) {if (num<16) {str_16+=switch_10_16 (num);    return 0;        } else {change (NUM/16);    Str_16+=switch_10_16 (NUM%16);    }}int Main () {long str_10;    cin>>str_10;    Change (STR_10);    cout<<str_16<<endl;   return 0; } 



A brief analysis of C + + shift operation and binary conversion

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.