gpt4 book ai didi

c++ - 从二进制编码中提取小时、分钟和秒

转载 作者:行者123 更新时间:2023-11-30 03:25:41 25 4
gpt4 key购买 nike

我写了一个程序来解决下面的练习。我弄对了小时和分钟,但我弄错了秒。

In order to save disk space Time field in the directory entry is 2 bytes long. Distribution of different bits which account for hours, minutes and seconds is given below:

15 14 13 12 11|10  9  8  7  6  5| 4  3  2  1  0
H H H H H| M M M M M M| S S S S S

Write a C++ Program that take input two-byte time entry and appropriately separates hours, minutes and seconds using suitable bitwise operators.

#include<iostream>
using namespace std;
int main()
{
unsigned int hours, mins, secs;
unsigned int time;
cout << "enter time ";
cin >> time;
hours = (time >> 11);
mins = ((time << 5) >> 10);
secs = ((time << 11) >> 11);
cout << hours << " " << mins << " " << secs << endl;
return 0;
}

为了得到分钟,我将输入向左移动 5 个位置,希望消除所有 H 位,然后向右移动 10 个位置以消除所有 S 位并且在最右边的位置有 M 位。

对于秒也是如此。

但是,如果我输入 445,我希望结果为 13 分 29 秒,但程序输出 0 13 445

为什么小时和分钟显示正确,但秒显示不正确?

最佳答案

secs = time & 0x1F; // This should give the 5 bits you're expecting for seconds.

关于c++ - 从二进制编码中提取小时、分钟和秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48868950/

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