A talk C Chestnut bar (33rd time: C-language example-skillfully use shift)

Source: Internet
Author: User
Tags greatest common divisor


Ladies and gentlemen, crossing, the last time we talked about the example of greatest common divisor, this time we say the example is: skillfully use shift. Gossip

Hugh, the words return to the positive turn. Let's talk C chestnuts together!


We talked about the bit operation in the 19th, and gave some simple examples. So there's not much to say here. If any crossing

Forget the bit operation related content, you can click here to find the original text.


What we're talking about today is another clever use of bit manipulation: using shifts for division operations . In fact, we also mentioned in the 19th

The multiplication method is calculated by shifting, but not specifically. So everyone may not have much impression, but it doesn't matter, from today onwards

You can get an impression of "divide using bit operations".


When you write a program, sometimes use the division operation, the division operation needs to note is: The divisor cannot be 0. So we're going to

It is common to judge whether the divisor is zero before the division operation. We use code to illustrate:

1 #include <stdio.h>

2

3 int Fun (int a, int b)

4 {

5 if (b = = 0) //Determine if divisor equals 0

6 return FALSE;

7 Else

8 return a/b;

9}


the trap of dividing the divisor to zero can be ignored when the shift operation is used instead of the division operation . Because there is no error after moving 0 bits to the divisor,

We say it's a clever way to use it. Here is the specific code:

1 #include <stdio.h>

2

3 int Fun (int a, int b)

4 {

5 return a>>b; //skillfully using shift, without judging whether the divisor equals 0

6}


Crossing, this ingenious use, in addition to eliminating the need to judge whether the divisor is zero, the performance of the program is higher than the usual division operation. When

However, this ingenious usage is not omnipotent and has its limitations:

    • 1. The object of the shift is preferably an unsigned number, if this is not guaranteed, then confirm that the compiler is a statement shift operation;
    • 2. The divisor is only 2 or 2 of the n-th square.


Crossing, clever use of displacement has both advantages, there are shortcomings, is so-called: nobody is perfect, let alone the program. You can rely on the actual situation

To use it. Like Mr. Lu Xun in the take doctrine advocated: take its essence, avoid its dross. Exhilaration?


Everybody, crossing, we're going to talk about the example of a clever shift. I want to know what the following example, and listen to tell.



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

A talk C Chestnut bar (33rd time: C-language example-skillfully use shift)

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.