gpt4 book ai didi

java - Java中的位总和

转载 作者:行者123 更新时间:2023-12-02 00:47:57 27 4
gpt4 key购买 nike

对不起 friend 们,我犯了一个错误。我又犯了这个错误。真的很抱歉。

这就是问题所在。

我的时间范围是这样的

int Starttime = 2 which mean(02:00)
int enttime = 8 which mean(08:00)

我想要时间的位总和,示例

     00:00 1  
01:00 2
02:00 4 -----
03:00 8 R
04:00 16 a
05:00 32 n
06:00 64 g
07:00 128 e
08:00 256 -----

很快就到23:00

所以我需要totalRange = 256+128+64+32+16+8+4;

应该是这样的再次抱歉。

谢谢

最佳答案

该表表明您想要使用此函数将时间的小时值映射到整数值:

int value = (int) Math.pow(2, hourValue);

或者换句话说,00:00h 将映射到 20,12:00h 将映射到 212,依此类推。

现在,如果您需要开始时间和结束时间的总和,您可以简单地使用上面的函数并添加值:

int totalrange = (int) Math.pow(2, starttime) + (int) Math.pow(2, endtime);

现在,如果您的 starttime=2 和 endtime=23,这将给出结果(以二进制形式编写):

01000000 00000000 00000100
<小时/>

无耻地适应polygenelubricants much faster solution :

int totalrange = (1 << starttime) + (1 << endtime);

这是有效的,因为 2i 等于 (1 << i)。

关于java - Java中的位总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3555022/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com