从二进制编码中提取小时、分钟和秒
Extracting hours, minutes and seconds from binary encoding
我写了一个程序来解决下面的练习。我弄对了小时和分钟,但我弄错了秒。
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
.
为什么小时和分钟显示正确,但秒显示不正确?
您假设 unsigned int 的大小为 16 位,但在当今的机器上通常将其实现为 32 位整数。
如果您使用 uint16_t 作为可变时间,它应该可以工作。
要使用 uint16_t 包括 header stdint.h。
#include <iostream>
#include <stdint.h>
using namespace std;
int main()
{
uint16_t hours, mins, secs;
uint16_t time = 445;
hours = (time >> 11);
mins = ((time << 5) >> 10);
secs = (time << 11);
secs >>= 11;
cout << hours << " " << mins << " " << secs << endl;
return 0;
}
(在 QT 5.5 上测试)
secs = time & 0x1F; // This should give the 5 bits you're expecting for seconds.
您应该将与您正在查看的时间部分不对应的位清零。我将使用二进制文字(在 C++14 中可用),但注释等效的十六进制文字。
struct time {
time(unsigned = 0); // also default constructs
unsigned hours;
unsigned mins;
unsigned secs;
}
time::time(unsigned packed) :
hours((packed & 0b1111100000000000) >> 11), // 0xF800
mins ((packed & 0b0000011111100000) >> 5 ), // 0x07E0
secs ((packed & 0b0000000000011111) ) // 0x001F
{ }
#include<iostream>
std::ostream& operator<< (std::ostream& os, time t) {
return os << t.hours << " " << t.mins << " " << t.secs;
}
int main()
{
time t(445);
std::cout << t << std::endl;
return 0;
}
给定的任务是不可能的,因为分钟 和 秒都需要 6 位。您已经为秒分配了 5 位。
2^5=32
2^6=64
使用此方案,需要 17 位来表示时间。
我写了一个程序来解决下面的练习。我弄对了小时和分钟,但我弄错了秒。
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
.
为什么小时和分钟显示正确,但秒显示不正确?
您假设 unsigned int 的大小为 16 位,但在当今的机器上通常将其实现为 32 位整数。
如果您使用 uint16_t 作为可变时间,它应该可以工作。
要使用 uint16_t 包括 header stdint.h。
#include <iostream>
#include <stdint.h>
using namespace std;
int main()
{
uint16_t hours, mins, secs;
uint16_t time = 445;
hours = (time >> 11);
mins = ((time << 5) >> 10);
secs = (time << 11);
secs >>= 11;
cout << hours << " " << mins << " " << secs << endl;
return 0;
}
(在 QT 5.5 上测试)
secs = time & 0x1F; // This should give the 5 bits you're expecting for seconds.
您应该将与您正在查看的时间部分不对应的位清零。我将使用二进制文字(在 C++14 中可用),但注释等效的十六进制文字。
struct time {
time(unsigned = 0); // also default constructs
unsigned hours;
unsigned mins;
unsigned secs;
}
time::time(unsigned packed) :
hours((packed & 0b1111100000000000) >> 11), // 0xF800
mins ((packed & 0b0000011111100000) >> 5 ), // 0x07E0
secs ((packed & 0b0000000000011111) ) // 0x001F
{ }
#include<iostream>
std::ostream& operator<< (std::ostream& os, time t) {
return os << t.hours << " " << t.mins << " " << t.secs;
}
int main()
{
time t(445);
std::cout << t << std::endl;
return 0;
}
给定的任务是不可能的,因为分钟 和 秒都需要 6 位。您已经为秒分配了 5 位。
2^5=32
2^6=64
使用此方案,需要 17 位来表示时间。