给定数字的b+树创建

Problem statement:

问题陈述:

Find Next and previous power of two of a given number

查找给定数字中两个的下一个和上一个幂

Next power of two

下一个二的幂

    Example(1):
input:  22
output: 32  ( as 32 is 2^5)
Example(2):
input: 54
output: 64  (as 64 is 2^6)

Previous power of two

以前的二的幂

    Example(1):
input:  22
output: 16
Example(2):
input:  54
output: 32

We can solve this problem using bit manipulation easily.

我们可以使用位操作轻松解决此问题。

Just have a look on the binary representation of the number which is a power of 2.

只需看一下数字的二进制表示形式就是2的幂。

    power of 2           Binary Representation
1                        1
2                        10
4                        100
8                        1000
16                       10000
32                       100000
64                       1000000
128                      10000000

As we can see every number have only their left most bit set (i.e 1) and rest are unset (i.e 0).

我们可以看到,每个数字只有最左边的位(即1)被置位,其余的都未置位(即0)。

求2的前次幂 (Finding previous power of 2)

If we somehow, can unset all bits except the left most bit in a binary representation of number we will get previous power of two of the given number.

如果我们以某种方式可以取消设置二进制数字表示形式中除最左边的位以外的所有位,我们将获得给定数字的2的幂。

Example:

例:

    Number           Binary representation          previous power of two
7                  111                             100    (i,e 4)
25                11001                           10000   (i.e 16)
95               1011111                         1000000 (i,e 64)

We will use Bitwise AND ( & ) operation to clear bits.

我们将使用按位与(&)操作清除位。

Here is Algorithm to get previous power of 2 of n,

这是获取n的2的先前幂的算法,

step 1: n = n & n-1
step 2: if n is power of two , n is our desired result,go to step 3.
else go to step 1.
step 3: print n and stop
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

Explanation:

说明:

let n   = 27 ( 11011 in binary)
n-1  = 26 ( 11010 in binary)
n&n-1 = 26
as 26 is not a power of 2 so we will repeat above step again
new n = 26
n   = 26  ( 11010 in binary)
n-1   = 25  ( 11001 in binary)
n&n-1 = 24  ( 11000 in binary)
as 24 is not a power of 2 so we will repeat above step again
new n=24
n   = 24  ( 11000 in binary)
n-1   = 23  ( 10111 in binary)
n&n-1 = 16  ( 10000 in binary)
as 16 is  a power of 2 so this is our answer

寻找二的下幂 (Finding next power of Two)

To get next power of two, all we have to do is to find a binary number greater than the given number having only left most bit set.

要获得2的下一个幂,我们要做的就是找到一个比给定数字大的二进制数,该二进制数只剩下最左边的位。

We can use left shift operator ( << )to generate a number having only its left most bit set.

我们可以使用左移运算符(<<)来生成仅设置其最左位的数字。

Left shift operation example:

左移操作示例:

1 << 5
This means that shift  1  left by 5 place
1 in binary is represented as  1
so after shifting 1 by 5 place left,
output will be 100000 ( i.e 32 in decimal)
5 << 2
This means that shift  5  left by 2 place
5 in binary is represented as  101
so after shifting it by 2 place to left,
output will be 10100 ( i.e 20 in decimal)

Note: In each shift right most bit will be set to 0;

注意:在每个移位右移的大多数位将被设置为0;

.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

Program:



程序:

</ s> </ s> </ s>

#include<bits/stdc++.h>
using namespace std;
long int  nextPowerOfTwo ( int  n )
{// Result is intialized as 1
long int value = 1;
// The following while loop will run until we
// get a number greater than n
while(value<=n)
{// value will be left shifted by 1 place in each iteration
value=value << 1;
}
return value ;
}
int  previousPowerOfTwo(int  n )
{// If n is already a power of two, we can simply shift it right
// by 1 place and get the previous power of 2
// (n&n-1) will be 0 if n is power of two ( for n > = 2 )
// (n&&!(n&(n-1))) condition will take care of n < 2;
if((n&&!(n&(n-1)))==1)
{return (n>>1);
}
// This while loop will run until we get a number which is a power of 2
while(n&n-1)
{// Each time we are performing Bit wise And ( & )operation to clear bits
n=n&n-1;
}
return  n ;
}
int main()
{int n;
cout << "Enter Number\n";
cin >> n;
long int num=nextPowerOfTwo(n);
cout << "Next power of two : " << num ;
cout << "\n\nEnter Number\n";
cin >> n;
num=previousPowerOfTwo(n);
cout << "Previous power of two : " << num ;
return 0;
}

Output

输出量

    Enter Number
34
Next power of two : 64
Enter Number
34
Previous power of two : 32
Process returned 0 (0x0)   execution time : 7.681 s
Press any key to continue.

翻译自: https://www.includehelp.com/cpp-programs/find-next-and-previous-power-of-two-of-a-given-number.aspx

给定数字的b+树创建

给定数字的b+树创建_在C ++中找到给定数字中的两个的下一个和上一个幂相关推荐

  1. 有数字要生成条形码生成器_如何制作自己的“意外”数字生成器

    有数字要生成条形码生成器 有时让人们感到惊讶的是,随机数生成是计算机科学中一个经典的著名问题,因为它看起来应该很容易. 只需选择一个数字. 但是,很难使计算机随机化. 许多Linux用户对/dev/r ...

  2. Python算法题----在列表中找到和为s的两个数字

    列表data的值为[1, 3, 4, 5, 8, 9, 11],找出这个列表中和为13的两个数字的所有组合.这个好找,上过幼儿园大班的,估计都能找出来.4+9=13, 5+8=13.如何用python ...

  3. java 字符串 数组 索引_如何在Java中找到数组中元素的索引?

    我希望在Java中找到给定元素的索引,知道它的内容. 我尝试了以下示例,该示例不起作用: class masi { public static void main( String[] args ) { ...

  4. java 查找链表中间元素_如何在Java中一次性查找Java中链表的中间元素

    如何在一次传递中找到LinkedList的中间元素?这是一个 Java 和非Java程序员面试时经常被问到的编程问题.这个问题类似于检查回文或计算阶乘,有时也会要求编写代码.为了回答这个问题,候选人必 ...

  5. 如何从JavaScript中的给定数字中形成最小的数字

    by Prashant Yadav 通过Prashant Yadav 如何从JavaScript中的给定数字中形成最小的数字 (How to form the smallest possible nu ...

  6. 蒙特卡洛树搜索_蒙特卡洛树是什么算法?

    点击上方"MLNLP",选择"星标"公众号 重磅干货,第一时间送达 编辑:忆臻 https://www.zhihu.com/question/39916945 ...

  7. java 分裂数字_分裂的补充:超越数字,打印物理可视化

    java 分裂数字 As noted in my earlier Nightingale writings, color harmony is the process of choosing colo ...

  8. scrape创建_确实在2分钟内对Scrape公司进行了评论和评分

    scrape创建 网页搜罗,数据科学 (Web Scraping, Data Science) In this tutorial, I will show you how to perform web ...

  9. b+树时间复杂度_数据结构:线性表,栈,队列,数组,字符串,树和二叉树,哈希表...

    作者:张人大 代码效率优化 复杂度 -- 一个关于输入数据量n的函数 时间复杂度 -- 昂贵 与代码的结构设计有着紧密关系 一个顺序结构的代码,时间复杂度是O(1), 即任务与算例个数 n 无关 空间 ...

最新文章

  1. Linux升级OpenSSH完整手册
  2. 开发日记-20190723 关键词 读书笔记《Linux 系统管理技术手册(第二版)》DAY 13
  3. 需求评审五个维度框架分析及其带来的启示-3-典型需求评审
  4. package.json---入门说明
  5. js字符串解析与转换成数字
  6. [PyTorch] torchvision库及其常用的函数
  7. JS 转换民族国标码(数字码和英文码)
  8. 文献检索与下载的几种方式
  9. 因代码不规范,国外程序员就枪击 4 个同事?你可能提前过了愚人节
  10. word 批量把参考文献的交叉引用变成上标形式
  11. python如何进行数据挖掘_如何使用python实现文本数据挖掘?
  12. The requested URL was not found on this server.
  13. 第四集:让声音变得更有磁性 —— 共鸣训练(汇播课程演说笔记)
  14. C++整数快速读写模板(快速读入+快速写)详解
  15. 在vue中使用three.js创建一个简单的立体图形
  16. 【历史上的今天】3 月 7 日:首条海底光缆开通;VeriSign 收购 Network Solutions;计算机图形学先驱诞生
  17. 欢迎使用Windows安装MySQL(安装版)教程,全网最细
  18. JavaScript内置对象(内置对象、查文档(MDN)、Math对象、日期对象、数组对象、字符串对象)
  19. 小学计算机课程目录五年级,小学五年级信息技术课件
  20. 什么?python dict字典有序了?!

热门文章

  1. log4jdbc mysql_[简单]log4jdbc-log4j2配置简记_MySQL
  2. Linux下导出MySQL为SQL文件_在linux命令下导出导入.sql文件的方法
  3. jmeter安装包双击没反应_windows环境下Jmeter5.2的安装使用
  4. 优先队列默认是小顶堆吗_一分钟带你读懂什么是堆?
  5. java mongodb 插入数据_mongoDB 插入数据 用java实现
  6. pcb天线和纯铜天线_如何简化天线设计?相控阵波束成形IC来助您
  7. c++ 二次开发 良田高拍仪_六枝特良田LYV-850加工中心导轨配套防护罩日常维修
  8. make找不到linux内核函数,linux内核make menuconfig出错
  9. assertpythonraise_使用assertRaise测试异常消息
  10. Juypter 打开其他路径文件