- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 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;
}
}
主要程序设计
n1
和n2
bits1
和bits2
,它们指向具有n1
和n2
位的数组。请注意,数组将按相反顺序排列,即最后一位将位于数组的第 0 个变量中。1
和 0
,进位是 1
,因此,返回将是 0
并进位将再次更改为 1
。sum
的数组中。sum
将使用我上面给出的函数转换为 int。注意: nand2tetris
项目是在 hdl
中完成的,但我很熟悉用 C 来完成。此外,我的许多功能上面提到的内容摘自stackoverflow。虽然,设计是我自己的。
最佳答案
两者都是二进制。区别是signed
或unsigned
。对于 >0
这是一样的。对于 <0
只要看最高位就可以看出它是一个负数。如果设置输出“-”并将负的二进制补码转换为其abs()
,则可以通过查看最高位轻松完成输出的相同功能。这可以很容易地按位完成。
注意:如果正数足够大以设置最高位,则无法再将其与负数的补码区分开。这就是为什么编程语言确实需要单独的类型(例如在 C int
和 unsigned
中)。
关于c - C语言中如何判断一个数是二进制还是2的补码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34768121/
在取反数字符号的两个补码中,您通常只需取反每一位并加 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
我是一名优秀的程序员,十分优秀!