0

0

设置最左边未设置的位

WBOY

WBOY

发布时间:2023-09-10 17:05:02

|

1220人浏览过

|

来源于tutorialspoint

转载

设置最左边未设置的位

本文旨在寻找一种设置给定数字最左边未设置位的方法。在最高位设置位之后的第一个未设置位被视为最左边的未设置位。

问题陈述

给定一个数字n,任务是将该数字的二进制展开中未设置的最左边的位设置为1。所有其他位应保持不变。如果原始数字的所有位都已设置,则返回该数字。

Examples

Input: 46
Output: 62

Explanation

的中文翻译为:

解释

Binary Expansion of 46 = 101110.

最左边未设置的位是 101110。

Upon setting the underlined bit we get, 111110. This is the binary expansion of 62.

Hence answer is 62.

Input: 11
Output: 15

Explanation

的中文翻译为:

解释

Binary Expansion of 11 = 1011.

最左边未设置的位是 1011。

Upon changing the underlined bit, we get 1111 which is the binary expansion of 15.

Input: 30
Output: 31

Explanation

的中文翻译为:

解释

Binary Expansion of 30 = 11110.

最左侧未设置的位为 11110。

在设置最左边未设置的位时,我们得到11111,这是31的二进制展开。

Input: 7
Output: 7

Explanation

的中文翻译为:

解释

Binary Expansion of 7 = 111.

由于所有位都被设置了,没有最左边未设置的位。因此答案与原始数字保持不变。

解决方案方法

  • 检查是否所有的位都被设置。如果是,则返回原始数字作为答案。

  • Find the position of the latest unset bit using bitwise AND operator, and update the counter.

  • 将位设置为与计数器相对应。

  • 显示答案。

查找是否所有位都被设置

The idea here is that by adding one bit, the input number will become a perfect square of 2 if all of its bits are set. Hence, the following expression will determine whether or not all the bits of the number are set: n & (n + 1) == 0;

Let us understand this through an example.

Let the number be 5. We need to check if all the bits of 5 are set or not.

的中文翻译为:
n = 3 n + 1 = 4 n & (n + 1)
011
011 100 000

Thus it is safe to conclude that all the bits of n are already set and we return the number as it is.

找到最左边未设置的位

如果AND操作的输出不等于零,则继续查找最左边未设置的位。首先生成一个位数与给定整数相等的数字。新数字的最左边位最初设置为1。

然后,我们运行一个循环,从最左边的1位开始,向右搜索第一个0,通过对给定数字和新数字进行位与运算来实现。当位与运算的结果为0时,我们返回第一个未设置的最左边位的位置pos。

To Set the Leftmost Unset Bit

Generate a new number in which only the bit corresponding to pos is set. Perform bitwise OR operation between this new number and the original number.

Algorithm

的中文翻译为:

算法

Function all_bits_set()

  • 计算 n & (n + 1)。

  • If result == 0, return true.

    网奇.NET商城系统
    网奇.NET商城系统

    网奇Eshop商城购物系统:集成国内优秀商城系统的成功元素,采用ASP.NET2.0语言设计开发.傻瓜式的管理模式,强大的后台管理,可添加或定制风格精美的模板,网站广告位任意添加,集成在线支付接口,内置简、繁、英三种语言.系统不断升级,力求尽善尽美.网奇商城的目标是:打造国内最到的商城系统! 升级功能:1.在线备份SQL数据库2.RSS在线订阅器3.整合了支付宝鲜花支付接口。4.整合了网奇E客通在

    下载
  • 否则返回 false。

Function find_leftmost_unset_bit()

  • Initialize m = 1, pos = 0.

  • while (n > m)

    • 左移 m 1 位

  • 将 m 右移 1 位,以使其对应于 n 的最高有效位。

  • while ((n & m) != 0)

    • 将 m 右移 1 位

    • pos++

  • 一旦循环中断,我们就可以得到最高有效位(MSB)中最左边未设置的位的位置。

  • 返回 log2(n) - pos,即从最低有效位开始的位位置。

函数 set_leftmost_unset_bit()

  • 初始化 k = 1

  • Function Call find_leftmost_unset_bit().

  • k = k

  • Compute n | k.

  • Update n.

Function main()

  • 初始化 n

  • Function Call all_bits_set()

  • 函数调用 find_leftmost_unset_bit()

  • 调用函数set_leftmost_unset_bit()

  • 显示 n

示例:C++程序

这个程序通过将输入数字 n 的二进制展开中最左边未设置的位设置为 1 来修改它。它使用位运算符 OR,左移和右移运算符以及位与运算符来实现其目标。

// A C++ program to set the left most unset bit of a number. If all the bits of the given number are already set, it returns the number as it is.
#include 
#include 
using namespace std;
// function to check if all bits of the given number are already set
// if all bits of n are set, n + 1 will be a power of 2.
bool all_bits_set(int n){
   if ((n & (n + 1)) == 0)    {
      return true;
   }
   return false;
}

// function to find the position of the leftmost unset bit from the LSB.
int find_leftmost_unset_bit(int n){
   int m = 1, pos = 0;
   while (n > m){
      m = m << 1;
   }
   m = m >> 1; // to make the number of digits in m equal to number of digits in n
   
   // the following loop executes till the first zero is encountered, starting from the msb
   while ((n & m) != 0){
      m = m >> 1;
      pos++;
   }
   
   // since pos is the position of the unset bit from the MSB we return log2(n) - pos which is the location of the leftmost unset bit from the LSB.
   return log2(n) - pos;
}

// function to set the leftmost unset bit from the LSB.
void set_leftmost_unset_bit(int &n){
   int k = 1;
   int pos = find_leftmost_unset_bit(n);
   k = k << (pos); // left shift k by pos
   n = n | k; // to set the leftmost unset bit
}

// main function
int main(){
   int n = 46;
   cout << "Input Number: "<< n << endl;
   if (all_bits_set(n))    {
      cout << n << endl;
      return 0;
   }
   set_leftmost_unset_bit(n);
   cout << "Number after setting the Leftmost Unset Bit: " << n << endl; // display the updated number
   return 0;
}

输出

Input Number: 46
Number after setting the Leftmost Unset Bit: 62

时间和空间分析

时间复杂度:O(log2(n)),因为在函数find_leftmost_unset_bit()中,我们可能需要遍历二进制展开式的所有log2(n)位数来找到最左边的未设置位。

Space Complexity: O(1), as constant space is always used in the implementation.

Conclusion

本文讨论了一种寻找并设置给定数字最左边未设置位的方法。如果数字的所有位已经设置,我们将返回该数字。否则,为了设置该位,我们使用位左移和右移运算符生成一个新的位模式,并使用位或运算符计算结果。解决方案的概念、多个示例、使用的算法、C++程序解决方案以及时间和空间复杂度分析都被详细解释,以便更深入地理解。

相关专题

更多
java基础知识汇总
java基础知识汇总

java基础知识有Java的历史和特点、Java的开发环境、Java的基本数据类型、变量和常量、运算符和表达式、控制语句、数组和字符串等等知识点。想要知道更多关于java基础知识的朋友,请阅读本专题下面的的有关文章,欢迎大家来php中文网学习。

1435

2023.10.24

Go语言中的运算符有哪些
Go语言中的运算符有哪些

Go语言中的运算符有:1、加法运算符;2、减法运算符;3、乘法运算符;4、除法运算符;5、取余运算符;6、比较运算符;7、位运算符;8、按位与运算符;9、按位或运算符;10、按位异或运算符等等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

222

2024.02.23

php三元运算符用法
php三元运算符用法

本专题整合了php三元运算符相关教程,阅读专题下面的文章了解更多详细内容。

84

2025.10.17

if什么意思
if什么意思

if的意思是“如果”的条件。它是一个用于引导条件语句的关键词,用于根据特定条件的真假情况来执行不同的代码块。本专题提供if什么意思的相关文章,供大家免费阅读。

711

2023.08.22

while的用法
while的用法

while的用法是“while 条件: 代码块”,条件是一个表达式,当条件为真时,执行代码块,然后再次判断条件是否为真,如果为真则继续执行代码块,直到条件为假为止。本专题为大家提供while相关的文章、下载、课程内容,供大家免费下载体验。

81

2023.09.25

function是什么
function是什么

function是函数的意思,是一段具有特定功能的可重复使用的代码块,是程序的基本组成单元之一,可以接受输入参数,执行特定的操作,并返回结果。本专题为大家提供function是什么的相关的文章、下载、课程内容,供大家免费下载体验。

470

2023.08.04

js函数function用法
js函数function用法

js函数function用法有:1、声明函数;2、调用函数;3、函数参数;4、函数返回值;5、匿名函数;6、函数作为参数;7、函数作用域;8、递归函数。本专题提供js函数function用法的相关文章内容,大家可以免费阅读。

158

2023.10.07

CSS position定位有几种方式
CSS position定位有几种方式

有4种,分别是静态定位、相对定位、绝对定位和固定定位。更多关于CSS position定位有几种方式的内容,可以访问下面的文章。

80

2023.11.23

桌面文件位置介绍
桌面文件位置介绍

本专题整合了桌面文件相关教程,阅读专题下面的文章了解更多内容。

0

2025.12.30

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
CSS教程
CSS教程

共754课时 | 17.2万人学习

微信小程序开发之API篇
微信小程序开发之API篇

共15课时 | 1.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号