日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/11 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 给定数字的b+树创建_在C ++中找到给定数字中的两个的下一个和上一个幂 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

給定數(shù)字的b+樹創(chuàng)建

Problem statement:

問題陳述:

Find Next and previous power of two of a given number

查找給定數(shù)字中兩個(gè)的下一個(gè)和上一個(gè)冪

Next power of two

下一個(gè)二的冪

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

Previous power of two

以前的二的冪

Example(1):input: 22output: 16Example(2):input: 54output: 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.

只需看一下數(shù)字的二進(jìn)制表示形式就是2的冪。

power of 2 Binary Representation1 12 104 1008 100016 1000032 10000064 1000000128 10000000

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

我們可以看到,每個(gè)數(shù)字只有最左邊的位(即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.

如果我們以某種方式可以取消設(shè)置二進(jìn)制數(shù)字表示形式中除最左邊的位以外的所有位,我們將獲得給定數(shù)字的2的冪。

Example:

例:

Number Binary representation previous power of two7 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 = 26as 26 is not a power of 2 so we will repeat above step again new n = 26n = 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=24n = 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的下一個(gè)冪,我們要做的就是找到一個(gè)比給定數(shù)字大的二進(jìn)制數(shù),該二進(jìn)制數(shù)只剩下最左邊的位。

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

我們可以使用左移運(yùn)算符(<<)來生成僅設(shè)置其最左位的數(shù)字。

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 << 2This 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;

注意:在每個(gè)移位右移的大多數(shù)位將被設(shè)置為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 1long int value = 1;// The following while loop will run until we // get a number greater than nwhile(value<=n){// value will be left shifted by 1 place in each iterationvalue=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 2while(n&n-1){// Each time we are performing Bit wise And ( & )operation to clear bitsn=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 Number34Next power of two : 64Enter Number34Previous power of two : 32Process returned 0 (0x0) execution time : 7.681 sPress any key to continue.

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

給定數(shù)字的b+樹創(chuàng)建

總結(jié)

以上是生活随笔為你收集整理的给定数字的b+树创建_在C ++中找到给定数字中的两个的下一个和上一个幂的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。