二进制补码
- 查看
- 提交
- 统计
- 提问
总时间限制:
5000ms
单个测试点时间限制:
1000ms
内存限制:
65536kB
描述
有符号的整数在计算机内部用二进制补码表示。
定义函数 to_complement(n, bits),对于给定的一个整数 n 和一个正整数 bits,该函数输出整数 n 的 bits 位二进制补码表示。
例如:
to_complement(5,8) 输出 00000101
to_complement(-5,8) 输出 11111011
to_complement(32767,16) 输出 0111111111111111
to_complement(-32767,16) 输出 1000000000000001
to_complement(-1,32) 输出 11111111111111111111111111111111
to_complement(13,3) 输出 13 is out of the range of representation with 3bits!
输入
第一行输入 m,表示接下来会有 m 行
每行输入两个用空格分隔的整数,分别表示 n 和 bits。
输出
m个0、1的序列
样例输入
2
5 8
-5 8
样例输出
00000101
11111011
提示
1.函数需要对参数情况进行检查;
2.不能使用内置函数 bin 以及字符串的 format 方法。
import math
def to_complement(n, bits):
if n > math.pow(2, bits - 1) - 1 or n