Embedded C Development---Use loops to move left to right

Source: Internet
Author: User
Shift N to the left m-bit int byte_to_left_move (int n, int m) {    int i, ret = 1;    if (n = = 0 | | n < 0)    {        return;        }    if (n = = 1)    {for        (i = 0; i < m; i++)            ret *= n * 2;    }    if (n > 1)    {for        (i = 0; i < m; i++)             ret *= n;        }    return ret;} Shift N to the right m-bit int byte_to_right_move (int n, int m) {    int i, ret = 1;    if (n = = 0 | | n < 0)    {        return;        }    if (n = = 1)    {       ret = 0;     }    if (n > 1)    {for        (i = 0; i < m; i++) {            n/= 2;        }        ret = n;    }    return ret;}
Idle bored, I wrote two API interface, test pass, but may not be perfect, but the initial implementation of a number to the left to move the N-bit algorithm, the algorithm is very simple, is the cycle of multiplication and divide, this time understand, in fact, the basis is very important, such as the beginning of the C language, The teacher teaches the multiplicative and cumulative algorithm, although simple, but still very large, these two APIs also have similar, such as left-shifted interface has the nature of the multiplicative on the inside.

Embedded C Development---Use loops to move left to right

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.