gpt4 book ai didi

c - C语言中如何判断一个数是二进制还是2的补码?

转载 作者:行者123 更新时间:2023-11-30 21:17:48 24 4
gpt4 key购买 nike

我正在用 C 语言仅使用逻辑门制作二进制加法器。例如,我想添加 4 + (-5),这样我就可以得到 2 的补码形式的答案,然后将其转换为十进制。同样,如果我这样做,4 + (-3) 我会得到二进制答案,并希望使用相同的函数将其转换为十进制。

现在,我知道如何将 2 的补码转换为十进制,将二进制转换为十进制。但我想使用相同的函数将 2 的补码和二进制转换为十进制。为此,我必须弄清楚该数字是二进制还是 2 的补码。这就是我被困住的地方。

有人能给我一个 C 语言的想法、算法或代码来找出一个数字是 2 的补码还是普通二进制吗?

源代码

芯片

// Author: Ashish Ahuja
// Date created: 8-1-2016
// Descriptions: This file stores all the chips for
// the nand2tetris project.
// Links: www.nand2tetris.org
// class.coursera.org/nand2tetris1-001
// Files needed to compile successfully: ourhdr.h

int not (unsigned int a) {
if (a == 1) {
return 0;
}
else if (a == 0) {
return 1;
}
}

int and (unsigned int a, unsigned int b) {
if (a == 1 && b == 1)
return 1;
else if ((a == 1 && b == 0) || (a == 0 && b == 1) || (a == 0 && b == 0))
return 0;
}

int nand (unsigned int a, unsigned int b) {
unsigned int ans = 10;
ans = and (a, b);
unsigned int ack = not (ans);
return ack;
}

int or (unsigned int a, unsigned int b) {
return (nand (not (a), not (b)));
}

int nor (unsigned int a, unsigned int b) {
return (not (or (a, b)));
}

int xor (unsigned int a, unsigned int b) {
unsigned int a_r;
unsigned int b_r;
unsigned int sra;
unsigned int srb;
a_r = not (a);
b_r = not (b);
sra = nand (a_r, b);
srb = nand (b_r, a);
return nand (sra, srb);
}

int xnor (unsigned int a, unsigned int b) {
return (not (xor (a,b)));
}

Ourhdr.h

include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <math.h>
#include <time.h>
#include <stdbool.h>
#include <termios.h>
#include <stddef.h>
#include <sys/types.h>
#include <my/signal.h>
#include <my/socket.h>
#include <my/io.h>
#include <my/lib.h>
#include <my/tree.h>
#include <my/bits.h>
#include <my/binary.h>
//#include <my/error.h>

#define MAXLINE 4096
#define BUFF_SIZE 1024

注意:我只会显示该项目所需的 header 。所以就认为其他 header 没有用。

将数组转换为整数的函数

int array_num (int arr [], int n) {
char str [6] [2];
int i;
char number [13] = {'\n'};
for (i = 0; i < n; i ++)
sprintf (str [i], "%d", arr [i]);
for (i = 0; i < n; i ++)
strcat (number, str [i]);
i = atoi (number);
return i;
}

获取 int 的位的函数,并返回指向包含位的数组的指针

int *get_bits (int n, int bitswanted) {
int *bits = malloc (sizeof (int) * bitswanted);
int k;
int mask;
int masked_n;
int thebit;
for (k = 0; k < bitswanted; k ++) {
mask = 1 << k;
masked_n = n & mask;
thebit = masked_n >> k;
bits [k] = thebit;
}
return bits;
}

将二进制转换为十进制的函数,反之亦然

int convert_num (int n, int what) {
int rem;
int i;
int binary = 0;
int decimal = 0;

switch (what) {
case 0: // Convert decimal to binary
i = 0;
rem = 0;
while (n != 0) {
rem = n % 2;
n /= 2;
binary += rem * i;
i *= 10;
}
return binary;
break;
case 1: // Convert binary to decimal
i = 0;
rem = 0;
while (n != 0) {
rem = n % 10;
n /= 10;
decimal += rem*pow (2, i);
i ++;
}
return decimal;
break;
}
}

主要程序设计

  • 从用户处读取两个数字n1n2
  • 获取指针bits1bits2,它们指向具有n1n2位的数组。请注意,数组将按相反顺序排列,即最后一位将位于数组的第 0 个变量中。
  • 放置一个 for 循环,在其中传递三个变量,即要添加的位并从上次添加位操作中携带。
  • 返回值为三位加进位,加后变为进位(如有)。例如-您传递 10,进位是 1,因此,返回将是 0 并进位将再次更改为 1
  • 返回结果将存储在另一个名为 sum 的数组中。
  • 数组 sum 将使用我上面给出的函数转换为 int。
  • 现在这就是我陷入困境的地方。我现在想将 int 更改为十进制数。但要做到这一点,我必须知道它是 2 的补码形式,还是普通的二进制形式。我不知道该怎么做。

注意: nand2tetris 项目是在 hdl 中完成的,但我很熟悉用 C 来完成。此外,我的许多功能上面提到的内容摘自stackoverflow。虽然,设计是我自己的。

最佳答案

两者都是二进制。区别是signedunsigned 。对于 >0这是一样的。对于 <0只要看最高位就可以看出它是一个负数。如果设置输出“-”并将负的二进制补码转换为其abs(),则可以通过查看最高位轻松完成输出的相同功能。这可以很容易地按位完成。

注意:如果正数足够大以设置最高位,则无法再将其与负数的补码区分开。这就是为什么编程语言确实需要单独的类型(例如在 C intunsigned 中)。

关于c - C语言中如何判断一个数是二进制还是2的补码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34768121/

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