n& (n-1)

Source: Internet
Author: User

1. Determine if a positive integer is a number of 2

Data comparison (uint_16 N;)

--------------------------------------------------------------------------------------------------------------- -----------------

Binary representation of a positive integer n positive integer n (n-1) positive integer (n-1) binary represents n& (n-1)

--------------------------------------------------------------------------------------------------------------- -----------------

2 0000000000000010 1) 0000000000000001 00000 00000000000

--------------------------------------------------------------------------------------------------------------- -----------------

4 0000000000000100 3) 0000000000000011 00000 00000000000

--------------------------------------------------------------------------------------------------------------- -----------------

- 0000000000010000 15 0000000000001111 00000 00000000000

--------------------------------------------------------------------------------------------------------------- ------------------

- 0000000001100100 0000000001100011 0000000001100000

--------------------------------------------------------------------------------------------------------------- ------------------

- 0000000010 000000 127 0000000001 11111 1 0000000000000000

--------------------------------------------------------------------------------------------------------------- ------------------

unsigned char ispower (unsigned int N)

{    return (0)  | | (N & (n10x000x01);}

2. Determine the number of the number "1" When a positive integer is converted to binary

Char int N) {    char0;      while (n)    {        &= n1;        I++    ;    } return i;}

n& (n-1)

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.