Same binary weight (bitwise operation)

Source: Internet
Author: User

Title Description The binary weight of a positive integer is the number of 1 ' s in its binary representation.for example,the decmial num ber 1 has a binary weight of 1,and the decimal number 1717 (which are 11010110101 in binary) have a binary weight of 7.Give A positive integer n,return the smallest integer greater than N that have the same binary weight as N.N would be between 1 a nd 1000000000,inclusive,the result is guaranteed to fit in a signed 32-bit interget. Enter the input have multicases and each CA SE contains a integer n. Output for each case,output the smallest integer greater than N which has the same binary weight as N. Example Input
17174712555555
Sample output
171881117555557

Exercises

1717 (0110 1011 0101), the next one is 1718 (0110 1011 0110)

767 (0010 1111 1111), the next one is 895 (0011 0111 1111)

348 (0001 0101 1100), the next one is 355 (0001 0110 0011)

It is not difficult to find a law, from the right of the first "01" Change to "10", and in "01" After all the "1" are moved to the end, in fact, this is the key point of the problem solving, then the core of the whole question to be transferred to these two sub-problems:

1. Change the first "01" from the right to "10"

2. Move all "1" after the "01" to the Last

1#include <cstdlib>2#include <cstring>3#include <algorithm>4#include <cstdio>5#include <cmath>6#include <iostream>7#include <vector>8#include <string>9#include <cstring>Ten#include <string.h> One#include <Set> A#include <queue> -#include <stack> -#include <map> the using namespacestd; -typedefLong LongLL; -  - intMain () + { -     intN; +      while(cin>>N) A     { at        intb=n& (-N);//get the last ' 1 ' to the last value in N -        intT=n+b;//generate carry, achieve ' 01 ' into ' Ten ' -        intS=t^n;//get the bits that change in the carry -        intk= (s>>2)/b;//after the first ' 10 ', do not change the two-bit, so move right 2 bits, and then ' 01 ' after the ' 1 ' to the last.  -        intN=t|k;//The final result is t|k. -cout<<n<<Endl; in     } -     return 0; to}

Same binary weight (bitwise operation)

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.