符号扩展1位2的补数? [英] sign extend 1-bit 2's complement number?

查看:185
本文介绍了符号扩展1位2的补数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名学生,我写在C函数符号扩展一个给定的位字段。我与32位工作。

I am a student and I am writing a function in C to sign extend a given bit field. I'm working with 32 bits.

我看了这个答案在谷歌,但没有发现什么,我一直在寻找。

I looked this answer up on Google but didn't find what I was looking for.

我在写函数返回一个或多个连续位三三两两complemnt重新presentation从32 INT拉动。最左边位为符号位。

I'm writing function that returns the twos complemnt representation of one or more consecutive bits pulled from a 32 int. The leftmost bit is the sign bit.

我怎么返回,如果我拉一个位?你如何重新present单位作为签署三三两两complment多少?

What do I return if I pull a single bit? How do you represent a single bit as a signed twos complment number?

推荐答案

这是没有多大意义谈了1位二进制补码。如果你只有1位是0或1。

It doesn't make much sense to talk a 1-bit two's complement number. If you only have 1 bit, it is either 0 or 1.

在一般情况下,如果你有N位二进制补码数的最高值前pressible是 2 ^(N-1)-1 和最低值为 -2 ^(N-1)。如果我们想成为愚蠢的,我们可以应用此N = 1,我们发现1位二进制补码数的范围从-1到0。

In general, if you have a two's complement number with N bits, the highest value expressible is 2^(N-1)-1 and the lowest value is -2^(N-1). If we want to be silly we can apply this to N=1 and we find that a 1-bit two's complement number can range from -1 to 0.

编辑:您写道:

我在写函数返回一个或多个连续位三三两两complemnt重新presentation从32 INT拉动。最左边位为符号位。

I'm writing function that returns the twos complemnt representation of one or more consecutive bits pulled from a 32 int. The leftmost bit is the sign bit.

我不认为这是很好的界定。的两补的是,可以让你重新present某个整数作为一系列比特的编码。给定一个整数,它会告诉你什么位来存储。我不知道二的补再一个位序列的presentation将是什么。这就像你问我给你一个系列任意字节的ASCII编码,或者你问我从法国的葛底斯堡演说翻译成英文。

I don't think this is well defined. Two's complement is an encoding that lets you represent a mathematical integer as a series of bits. Given an integer, it tells you what bits to store. I don't know what the two's complement representation of a series of bits would be. It's like you're asking me to give you the ASCII encoding of a series of arbitrary bytes, or you are asking me to translate the Gettysburg Address from French to English.

这篇关于符号扩展1位2的补数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆