Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 具有一个零位的下一个更高的数字_C++_Binary_Bit Manipulation - Fatal编程技术网

C++ 具有一个零位的下一个更高的数字

C++ 具有一个零位的下一个更高的数字,c++,binary,bit-manipulation,C++,Binary,Bit Manipulation,今天我遇到了这个问题,但过了一段时间我还是解决不了。我需要一些帮助 我有数字N。问题是找到下一个更高的数字(>N),在二进制中只有一个零位 例如: 数字1可以用二进制表示为1。 下一个只有一个零位的更高数字是2-二进制10 其他几个例子: N=2(10),下一个更高的数字和一个零位是5(101) N=5(101),下一个更高的数字是6(110) N=7(111),下一个更高的数字是11(1011) 200人名单: 1 1 2 10 - 1 3 11 4 100 5 101 - 1 6 110 -

今天我遇到了这个问题,但过了一段时间我还是解决不了。我需要一些帮助

我有数字N。问题是找到下一个更高的数字(>N),在二进制中只有一个零位

例如: 数字1可以用二进制表示为1。 下一个只有一个零位的更高数字是2-二进制10

其他几个例子:
N=2(10)
,下一个更高的数字和一个零位是
5(101)

N=5(101)
,下一个更高的数字是
6(110)

N=7(111)
,下一个更高的数字是
11(1011)

200人名单:

1 1
2 10 - 1
3 11
4 100
5 101 - 1
6 110 - 1
7 111
8 1000
9 1001
10 1010
11 1011 - 1
12 1100
13 1101 - 1
14 1110 - 1
15 1111
16 10000
17 10001
18 10010
19 10011
20 10100
21 10101
22 10110
23 10111 - 1
24 11000
25 11001
26 11010
27 11011 - 1
28 11100
29 11101 - 1
30 11110 - 1
31 11111
32 100000
33 100001
34 100010
35 100011
36 100100
37 100101
38 100110
39 100111
40 101000
41 101001
42 101010
43 101011
44 101100
45 101101
46 101110
47 101111 - 1
48 110000
49 110001
50 110010
51 110011
52 110100
53 110101
54 110110
55 110111 - 1
56 111000
57 111001
58 111010
59 111011 - 1
60 111100
61 111101 - 1
62 111110 - 1
63 111111
64 1000000
65 1000001
66 1000010
67 1000011
68 1000100
69 1000101
70 1000110
71 1000111
72 1001000
73 1001001
74 1001010
75 1001011
76 1001100
77 1001101
78 1001110
79 1001111
80 1010000
81 1010001
82 1010010
83 1010011
84 1010100
85 1010101
86 1010110
87 1010111
88 1011000
89 1011001
90 1011010
91 1011011
92 1011100
93 1011101
94 1011110
95 1011111 - 1
96 1100000
97 1100001
98 1100010
99 1100011
100 1100100
101 1100101
102 1100110
103 1100111
104 1101000
105 1101001
106 1101010
107 1101011
108 1101100
109 1101101
110 1101110
111 1101111 - 1
112 1110000
113 1110001
114 1110010
115 1110011
116 1110100
117 1110101
118 1110110
119 1110111 - 1
120 1111000
121 1111001
122 1111010
123 1111011 - 1
124 1111100
125 1111101 - 1
126 1111110 - 1
127 1111111
128 10000000
129 10000001
130 10000010
131 10000011
132 10000100
133 10000101
134 10000110
135 10000111
136 10001000
137 10001001
138 10001010
139 10001011
140 10001100
141 10001101
142 10001110
143 10001111
144 10010000
145 10010001
146 10010010
147 10010011
148 10010100
149 10010101
150 10010110
151 10010111
152 10011000
153 10011001
154 10011010
155 10011011
156 10011100
157 10011101
158 10011110
159 10011111
160 10100000
161 10100001
162 10100010
163 10100011
164 10100100
165 10100101
166 10100110
167 10100111
168 10101000
169 10101001
170 10101010
171 10101011
172 10101100
173 10101101
174 10101110
175 10101111
176 10110000
177 10110001
178 10110010
179 10110011
180 10110100
181 10110101
182 10110110
183 10110111
184 10111000
185 10111001
186 10111010
187 10111011
188 10111100
189 10111101
190 10111110
191 10111111 - 1
192 11000000
193 11000001
194 11000010
195 11000011
196 11000100
197 11000101
198 11000110
199 11000111
200 11001000
有三种情况

  • 数字
    x
    在其二进制表示中有一个以上的零位。除一个零位外,所有零位都必须用1“填充”,以获得所需的结果。请注意,与仅填充最上面的零位获得的数字相比,通过获取
    x
    并填充其一个或多个低阶零位获得的所有数字在数字上更接近
    x
    。因此,答案是数字
    x
    ,除一个零位外,其余零位均已填充:只有其最顶端的零位保持未填充状态。例如,如果
    x=110101001
    ,则答案是
    110111111
    。要获得答案,请找到
    x
    的最高零位的索引
    i
    ,然后计算
    x
    2^i-1
    的按位OR
  • C本案例的代码:

    // warning: this assumes x is known to have *some* (>1) zeros!
    unsigned next(unsigned x)
    {
      unsigned topmostzero = 0;
      unsigned bit = 1;
      while (bit && bit <= x) {
          if (!(x & bit)) topmostzero = bit;
          bit <<= 1;
      }
      return x | (topmostzero - 1);
    }
    
    //警告:假设已知x有*个*(>1)零!
    未签名的下一个(未签名的x)
    {
    无符号topmostzero=0;
    无符号位=1;
    
    虽然(bit&&bit不能错过记住二进制逻辑的机会:),但我的解决方案如下:

    这是主要的

    main(int argc, char** argv)
    {
       int i = 139261;
       i++;
       while (!oneZero(i))
       {
          i++;
       }
       std::cout << i;
    }
    

    您还可以使用另一种方法:

    每个正好有一个零位的数字可以表示为 2^n-1-2^m

    现在任务很简单: 1.找到一个n,它至少可以容纳2^n-1-2^0>x,相当于2^n>x+2 2.找出2^n-1-2^m仍然大于x的最大m

    作为代码:

    #include <iostream>
    #include <math.h>
    using namespace std;
    
    //binary representation
    void bin(unsigned n)
    {
        for (int i = floor(log2(n));i >= 0;--i)
          (n & (1<<i))? printf("1"): printf("0");
    }
    
    //outputs the next greater int to x with exactly one 0 in binary representation
    int nextHigherOneZero(int x)
    {
      unsigned int n=0;
      while((1<<n)<= x+2 ) ++n; 
      unsigned int m=0;
      while((1<<n)-1-(1<<(m+1)) > x && m<n-2)
        ++m;
      return (1<<n)-1-(1<<m);
    }
    
    int main() 
    {
      int r=0;
      for(int i = 1; i<100;++i){
        r=nextHigherOneZero(i);
        printf("\nX: %i=",i);
        bin(i);
        printf(";\tnextHigherOneZero(x):%i=",r);
        bin(r);
        printf("\n");
      }
      return 0;
      }
    
    #包括
    #包括
    使用名称空间std;
    //二进制表示
    空箱(未签名的n)
    {
    对于(inti=floor(log2(n));i>=0;--i)
    
    (n&(1)您当前的方法是?@AdrianoRepetti我不熟悉位操作,所以我选择尝试字符串。我可以看到,下一个更高的数字有一个零位,右边只有一个零。从数字
    9(1001)
    开始,下一个更高的数字有一个零位是
    11(1011)
    ,下一个是
    1101、1110
    10111
    ,依此类推……但我需要一种正确的位操作方式(如果存在的话)你能详细说明得到答案的部分吗?找到x的最顶端零的索引i,然后计算x和2^i-1的按位OR,因为我试过了,但没有得到正确的答案。我得到了,但仍然不确定什么是“最顶端零”?从左侧开始的第一个零位。例如,如果
    x=1100101
    ,则它是从左侧开始的第三位。
    topmostzero
    是一个变量,在该位置只有1个。对于这个示例,
    x
    topmostzero=10000
    ,是二进制的。很高兴我最近想出了自己的方法。我想你的解决方案不够快。这相当于计算字符串中的零位。是否要打赌哪个更快?:)
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    //binary representation
    void bin(unsigned n)
    {
        for (int i = floor(log2(n));i >= 0;--i)
          (n & (1<<i))? printf("1"): printf("0");
    }
    
    //outputs the next greater int to x with exactly one 0 in binary representation
    int nextHigherOneZero(int x)
    {
      unsigned int n=0;
      while((1<<n)<= x+2 ) ++n; 
      unsigned int m=0;
      while((1<<n)-1-(1<<(m+1)) > x && m<n-2)
        ++m;
      return (1<<n)-1-(1<<m);
    }
    
    int main() 
    {
      int r=0;
      for(int i = 1; i<100;++i){
        r=nextHigherOneZero(i);
        printf("\nX: %i=",i);
        bin(i);
        printf(";\tnextHigherOneZero(x):%i=",r);
        bin(r);
        printf("\n");
      }
      return 0;
      }