# Define lowbit (x) & amp; (-x,

Source: Internet
Author: User
Tags decimal to binary

# Define lowbit (x) & (-x) Principle details,

# Define lowbit (x) & (-x ))

It can also be written as follows:

Int Lowbit (x ){

Return x & (-x );

}

For example:

1> x = 1:

Convert decimal to binary (8 digits ):

1 => 0000 0001

-1 => 1111 1111(Here is the complement of 1)

1 & (-1 ):

2> x = 6:

Convert decimal to binary (8 digits ):

6 => 0000 0110

-6 => 1111 1010(Here is the complement Code of 6)

6 & (-6 ):

So 6 & (-6) = 2

Summary:

Obtain 2 ^ p (where p: x represents the first position of 1 in the right direction of the number ),

For example, if the binary value of 6 is 110, the zeroth to the left is 0, and the first is 1, p = 1,

Therefore, Lowbit (6) = 2 ^ 1 = 2.

It can be directly understood as: Binary bitwise and operation, returns the maximum power factor of 2 not greater than x

StartAt: 2016-03-18, 16:37:32

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.