- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此有一个可以用 16 位表示的有符号十进制数,在 char* 中。我想要在 2 补码二进制中拥有该数字的 char* 。所以我想在C中从“-42”到“1111111111010110”(注意显示所有16位)。有没有一种快速而肮脏的方法来做到这一点?也许是一些库函数?或者我必须自己编写一个大型函数才能做到这一点?
我知道 strtol() 可能有一些用处。
最佳答案
没有一个标准库函数可以生成您所描述的二进制字符串。
不过,这并不是特别难做到。
#include <stdio.h>
#include <stdint.h>
#include <ctype.h>
int main(int argc, char ** argv)
{
while(--argc >= 0 && ++argv && *argv){
char const * input = *argv;
if(! input)
continue;
uint32_t value = 0;
char negative = 0;
for(; *input; input++){
if (isdigit(*input))
value = value * 10 + (*input -'0');
else if (*input == '-' && value == 0)
negative = 1;
else {
printf("Error: unexpected character: %c at %d\n", *input, (int)(input - *argv));
continue; // this function doesn't handle floats, or hex
}
}
if (value > 0x7fff + negative){
printf("Error: value too large for 16bit integer: %d %x\n", value, value);
continue; // can't be represented in 16 bits
}
int16_t result = value;
if (negative)
result = -value;
for (int i=1; i <= 16; i++)
printf("%d", 0 != (result & 1 << (16-i)) );
printf("\n");
}
}
该函数处理所有有效的 16 位值,并利用架构将整数存储为二进制补码值的事实。我不知道有哪个架构不这样做,所以这是一个相当合理的假设。
请注意,二进制补码 INT_MIN != -1 * INT_MAX。这是通过在从无符号 32 位转换为有符号 16 位之前向有效性检查添加否定标志来处理的。
./foo 1 -1 2 -2 42 -42 32767 -32767 32768 -32768
0000000000000001
1111111111111111
0000000000000010
1111111111111110
0000000000101010
1111111111010110
0111111111111111
1000000000000001
Error: value too large for 16bit integer: 32768 8000
1000000000000000
关于有符号十进制数的 char* 到 2s 补码二进制表示的 char*?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42426946/
在取反数字符号的两个补码中,您通常只需取反每一位并加 1。 例如: 011 (3) 100 + 1 = 101 (-3) 在 VHDL 中是: a <= std_logic_vector(unsign
我有这个代码: package com.company; import java.net.InetAddress; import java.net.UnknownHostException; publ
这个问题在这里已经有了答案: How do I perform a bitwise NOT in SSE/AVX? (4 个答案) 关闭 5 年前。 在 AVX2 中似乎没有按位非/补的内在函数。我
我是初学者。我正在尝试获取存储在字符串中的二进制数的 2 的补码sou2_reg='000000000000000000000000000000011'。如果我执行 sou2_reg[32],它会给我
我决定这样做 翻转数字 0=1, 1=0 LSB加1 如果进位,循环直到array[i]==0 但我坚持最后一点;我怎么能在条件循环中这样说呢? 最佳答案 你在谈论扩展算术。大多数处理器都有来自每个加
前言 补码是给机器看的,原码是给人看的,反码是二者的桥梁,原码反码补码虽然是简单问题,但确实很多人很长时间没有搞明白和深入思考,这篇把自己学习和理解过程记录下来,刚好一个学妹问
我运行一个内部应用程序,使用 visual studio 2008 编程,使用 SQL Server 2008 作为后端。除了服务器之外,我的用户还要求能够将数据存储在独立的文件中。列举的原因包括:
UInt16 Checksum16Calculate(CHECKSUM_16_TYPE* pChecksum) { //calculate twos compliment of checksum pC
这是我目前正在做的家庭作业。我们要做的就是查看传递的 32 位 int x 并返回以 2 补码形式存储该值所需的最少位。 例如: howManyBits(0) = 1; howManyBits(-1)
我想对 sum+ 中的任何位进行 1s 补码,并将补码的位保存在 finalsum 中。怎么做。我对使用 bitvec 和 uint32_t 类型的东西有点虚弱。所以我在这里很困惑。请帮忙。 #inc
我找到的所有关于如何找到 1(翻转正二进制位)和 2(翻转正二进制位并加 1)补码的答案似乎都没有回答我的问题。 我的家庭作业要求找到负数的补码。因此,我不是从正数开始,而是需要找出它的负数是什么,而
我正在用 C 语言编写一个模拟器,我想让 constantValuable ,即 65530 (0xFFFA) 成为 5 的二进制补码变量,但我似乎不太正确。下面是我希望完成此操作的 if 语句的示例
我正在使用 Visual Studio 2013。 最近我尝试了 ~ 运算符来求 1 的补码: int a = 10; cout << ~a << endl; 输出为-11 但是为了 unsigned
我这里有一个主题,来自“数字系统”,主题为“计算机组织与体系结构简介” 然后我遇到了这个主题,“自我补充代码” 它分为三个部分,如下所示: i)多余3(我理解这一部分,因为它要求我们在BCD中加3)
我有 -9 作为一个整数,我如何将其转换为 C 中的 5 位 2 补码整数?本质上是得到 10111? 我当前的代码是什么样的: char src2[3] = "-9"; int int_src2 =
The dataFile.bin is a binary file with 6-byte records. The first 3 bytes of each record contain the
我是一名优秀的程序员,十分优秀!