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

给定数字的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

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;

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+树创建

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542668.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java 字节数组作用_这段java代码中字节数组b起到了什么作用?

importjava.io.*;importjavax.swing.*;publicclassIOMonitor{publicstaticvoidmain(String[]temp){//TODO自动生成的方法存根byteb[]newbyte[2];try{FileInputStreamfisnewFileInput...import java.io.*;import javax.swing.*;public class IOMonitor {public static void main…

如何查看本地的崩溃log_过年回家,还怕抢不到票?程序员教你如何抢票

2019年接近尾声&#xff0c;距离春节回家的日子越来越近&#xff0c;26日起&#xff0c;2020年除夕火车票正式开售&#xff0c;抢票大战也进入白热化阶段。是否为某抢票 App 加速而烦恼&#xff0c;是否为车票“秒光而烦恼”。别慌&#xff0c;作为连“对象”都是 new 出来的程…

获取列表中包含的元素数 在C#中

Given a list, and we have to count its total number of elements using List.Count property. 给定一个列表&#xff0c;我们必须使用List.Count属性计算其元素总数 。 C&#xff03;清单 (C# List) A list is used to represent the list of the objects, it is represent…

I00037 亏数(Deficient number)

数论中&#xff0c;若一个正整数除了本身之外所有因子之和比此数自身小&#xff0c;则称此数为亏数。亏数&#xff08;Deficient number&#xff09;也称为缺数&#xff0c;参见百度百科_亏数&#xff0c;或参见维基百科的Deficient number。亏数在OEIS中的数列号为A005100。 问…

hashmap转红黑树的阈值为8_面试必考的 HashMap,这篇总结到位了

点击蓝色“JavaKeeper”关注我哟加个“星标”&#xff0c;一起成长&#xff0c;做牛逼闪闪的技术人1 概述HashMap是基于哈希表实现的,每一个元素是一个key-value对,其内部通过单链表解决冲突问题,容量不足(超过了阀值)时,同样会自动增长.HashMap是非线程安全的,只适用于单线程环…

linux用户组管理命令_Linux用户和组命令能力问题和解答

linux用户组管理命令This section contains Aptitude Questions and Answers on Linux User and Group Commands. 本节包含有关Linux用户和组命令的 Aptitude问答。 1) Which of the following commands is used to create a new user in the Linux operating system? create…

Failed to start firewalld.service: Unit firewalld.service is masked.

2019独角兽企业重金招聘Python工程师标准>>> FireWall in Centos 7 masked How to resolve the error message belowFailed to issue method call: Unit firewalld.service is masked. The main reason a service is masked is to prevent accidental starting or e…

mysql第二个索引_MySQL高级第二章——索引优化分析

一、SQL性能下降原因1.等待时间长&#xff1f;执行时间长&#xff1f;可能原因&#xff1a;查询语句写的不行索引失效(单值索引、复合索引)CREATE INDEX index_user_name ON user(name);(底层做了一个排序)CREATE INDEX index_user_nameEmail ON user(name,email);查询关联join…

递归反转链表改变原链表吗_在不使用递归的情况下找到链表的长度

递归反转链表改变原链表吗Solution: 解&#xff1a; Algorithm to find length 查找长度的算法 Input: 输入&#xff1a; A singly linked list whose address of the first node is stored in a pointer, say head. 一个单链表 &#xff0c;其第一个节点的地址存储在指针(例…

西瓜仿站高手v1.08官方正式版

2019独角兽企业重金招聘Python工程师标准>>> 西瓜仿站高手是一款绿色好用的由追风网络出品的网站模板批量下载软件&#xff0c;西瓜仿站高手是一款仿站工具&#xff0c;仿站神器。软件功能强大&#xff0c;能够帮你轻松帮你下载任意网站、任意模板&#xff0c;并且速…

用hundred造句子_八个有趣的开学破冰游戏,线上线下都能用

知道大家最近都很忙&#xff0c;所以省略开篇&#xff0c;直接上正题——开学“破冰游戏”走起&#xff01;一、你比划我来猜把词语展示在PPT上&#xff0c;猜词的同学背对PPT&#xff0c;其他同学可以看到词语并且用身体动作把词语表现出来&#xff0c;直到猜词的同学可以把词…

java 执行顺序_Java代码执行顺序

程序中代码执行的顺序非常重要&#xff0c;稍有不慎便会是程序运行出错&#xff0c;那么我将结合实例来分析代码中的执行。名词解释首先了解几个名词&#xff1a;非静态代码块直接由 { } 包起来的代码&#xff0c;称为非静态代码块静态代码块直接由 static { } 包起来的代码&am…

mysql 包含的那些文件

*.frm是描述了表的结构 *.MYD保存了表的数据记录 *.MYI则是表的索引 ibd是MySQL数据文件、索引文件&#xff0c;无法直接读取。 转载于:https://www.cnblogs.com/07byte/p/5823667.html

math 计算float_Java Math类静态float min(float f1,float f2)与示例

math 计算float数学类静态浮点数min(float f1&#xff0c;float f2) (Math Class static float min(float f1 , float f2) ) This method is available in java.lang package. 此方法在java.lang包中可用。 This method is used to return the minimum one of both the given a…

vector 不初始化时什么状态_Vue原理解析(三):初始化时created之前做了什么?...

让我们继续this._init()的初始化之旅&#xff0c;接下来又会执行这样的三个初始化方法&#xff1a;initInjections(vm) initState(vm) initProvide(vm)5. initInjections(vm): 主要作用是初始化inject&#xff0c;可以访问到对应的依赖。inject和provide这里需要简单的提一下&a…

switch 字符串 java_JDK7新特性switch支持字符串

在JDK7中,switch语句的判断条件增加了对字符串类型的支持。由于字符串的操作在编程中使用频繁,这个新特性的出现为Java编程带来了便利。接下来通过一个案例演示一下在switch语句中使用字符串进行匹配。public class Example {public static void main(String[] args) {String w…

cisco packet tracer路由器配置_【干货】思科交换机路由器怎么配置密码?

今天带大家看看如何在思科的交换机路由器当中配置安全特性&#xff0c;也就是密码的配置方式。在学习配置之前&#xff0c;我们先回顾一下密码相关知识。密码学是研究信息系统安全保密的科学。人类有记载的通信密码始于公元前400年&#xff0c;古希腊人是置换密码学的发明者。密…

perl 哈希数组的哈希_使用哈希检查两个数组是否相似

perl 哈希数组的哈希Prerequisite: Hashing data structure 先决条件&#xff1a; 哈希数据结构 Problem statement: 问题陈述&#xff1a; Check whether two arrays are similar or not using the hash table. The arrays are of the same size. 使用哈希表检查两个数组是否…

codevs3872 邮递员送信(SPFA)

邮递员送信 时间限制: 1 Sec 内存限制: 64 MB提交: 10 解决: 5[提交][状态][讨论版] 题目描述 有一个邮递员要送东西&#xff0c;邮局在节点1.他总共要送N-1样东西&#xff0c;其目的地分别是2~N。由于这个城市的交通比较繁忙&#xff0c;因此所有的道路都是单行的&#xff0…

java上传csv文件上传_java处理csv文件上传示例详解

前言&#xff1a;示例只是做了一个最最基础的上传csv的示例&#xff0c;如果要引用到代码中去&#xff0c;还需要根据自己的业务自行添加一些逻辑处理。readcsvutil工具类package com.hanfengyeqiao.gjb.utils;import java.io.*;import java.util.*;/*** csv工具类*/public cla…