【进制问题】取某个十进制数(二进制已知为8位)的二进制形式的低4位与高3位的方法

十进制数m有01234567共8位
取123位置的二进制数方法

(m >> 4) & 0x07

取4567位置的二进制数方法

m & 0x0f

例题 响应报文时间

image

样例1
输入

3  
0 20  
1 10  
8 20

输出

11

样例2
输入

2  
0 255  
200 60

输出

260

样例3
输入

1
5 100

输出

105

C++代码

#include <iostream>

using namespace std;

const int N = 1e5 + 10;

int n, res = N;

int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    while (n--)
    {
        int t, m;
        cin >> t >> m;
        if (m < 128)
        {
            res = min(res, t + m);
        }
        else
        {
            int exp = (m >> 4) & 0x07;// 取高三位
            int mant = m & 0x0f; // 取低四位
            res = min(res, (mant | 0x10) << (exp + 3));
        }
    }
    cout << res;
    return 0;
}
posted @ 2025-02-14 23:35  Tshaxz  阅读(27)  评论(0)    收藏  举报
Language: HTML