2个回答

qq_34200818 反啦，我要的是输入二进制补码 输出其十进制数 你的这个是十进制数转为二进制补码

java-输出给定整数i的二进制补码

c++针对二进制补码算术中整数的算术运算问题，请大神指点

c++ 二进制问题请教 以下程序针对二进制补码算术中整数的算术运算问题，要求先确定一个bit pattern size ，然后第一个函数要把输入的两个整数转化为二进制输出，第二个函数把两个二进制数相加输出，第三个函数再把结果转化回十进制。 只使用c++字符串数据类型，不使用任何数组变量。不改变主程序也不更改函数名，实现缺失函数，使程序运行正确。可编写额外的帮助函数，从函数中调用但不能对程序做修改。 需要使用c++字符串数据类型来表示这两个二进制补码的表示形式，不能使用任何数组变量，可使用字符串变量作为一个整体或使用索引访问字符串中变量中的字符。 程序示例运行如下 输入位模式大小的正整数：5 输入整数A：9 输入整数B：-14 十进制 9 + -14 = -5 二进制补码9 是 01001 二进制补码-14 是 10010 01001和10010的二进制和是11011。 在二进制补码计算中，9＋ -14=-5。 #include iostream> #include cmath> #include string> using namespace std; int main() { //Read in the bit pattern size int L; do { cout << "Enter positive integer for the bit pattern size "; cin >> L; }while (L <= 0); //Read in two integers a and b int a, b; cout << "Enter an integer a "; cin >> a; cout << "Enter an integer b "; cin >> b; //Calculate the decimal arithmetic sum of a and b and print the result int c1 = a + b; cout << "In decimal " << a << " + " << b << " is " << c1 << endl; //Compute the two's complement representations of a and b //Each integer must be represented in L-bits pattern //Also these two's complement representations must be returned as string data types string A = decimalToTwocomplementString(a, L); string B = decimalToTwocomplementString(b, L); //Print the two's complement representations of a and b cout << "The two's complement of " << a << " is\t " << A << endl; cout << "The two's complement of " << b << " is\t " << B << endl; //Compute the binary sum of the two's complement representations of a and b //The result must be returned as L-bit pattern string data type string C = TwoComplementStringAddition(A, B); //Print the two's complement representation binary sum cout << "The binary sum of " << A << " and " << B << " is " << C << endl; //Convert the two's complement representation binary sum to decimal and print int c2 = TwoComplementStringToDecimal(C); cout << "In two's complement arithmetic, " << a << " + " << b << " is " << c2 << endl; //Print some concluding results if (c1 == c2) cout << c1 << " is equal to " << c2 << ". Good Job!" << endl; else { cout << c1 << " is not equal to " << c2 << endl; cout << "Either " << c1 << " cannot be represented by the given bit pattern OR we have made some mistake!" << endl; } system("Pause"); return 0; } **_原程序拷屏如下：_** ![图片说明](https://img-ask.csdn.net/upload/201807/06/1530848793_765945.jpg) ![图片说明](https://img-ask.csdn.net/upload/201807/06/1530848804_675124.jpg)

5位二进制补码的最高（大）有符号的整数和最低（小）有符号的整数（可能是负数）？

Golang：二进制补码和fmt.Printf

<div class="post-text" itemprop="text"> <p>So computers use Two's complement to internally represent signed integers. I.e., -5 is represented as ^5 + 1 = "1111 1011".</p> <p>However, trying to print the binary representation, e.g. the following code:</p> <pre><code>var i int8 = -5 fmt.Printf("%b", i) </code></pre> <p>Outputs <code>-101</code>. Not quite what I'd expect. Is the formatting different or is it not using Two's complement after all?</p> <p>Interestingly, converting to an unsigned int results in the "correct" bit pattern:</p> <pre><code>var u uint8 = uint(i) fmt.Printf("%b", u) </code></pre> <p>Output is <code>11111011</code> - exactly the 2s complement of <code>-5</code>.</p> <p>So it seems to me the value is internally the really using Two's complement, but the formatting is printing the unsigned <code>5</code> and prepending a <code>-</code>. </p> <p>Can somebody clarify this?</p> </div>

vb.net 怎么将十进制转为二进制补码？

design a new complement-based binary encoding scheme that encodes integers in the range of -68~187 in 8 bits 没理解题目。。为什么会出现这样的范围啊。。

``` data segment buff1 db 20 ;存放最大字符个数 n1 db ? ;实际存放字符个数 content1 db 20 dup(0) ;存放输入字符 buff2 db 20 ;buf=buffer缓冲区，content目录 n2 db ? content2 db 20 dup(0) buff4 db 21 dup(0) ;buff3用于存放结果 data ends code segment assume cs:code,ds:data start: mov ax,data mov ds,ax buffa: mov dx,offset buff1 mov ah,0ah int 21h mov dl,0ah mov ah,2 int 21h buffb: mov dx,offset buff2 mov ah,0ah int 21h mov cl,n1 mov si,offset content1 transfer1: sub byte ptr[si],30h inc si loop transfer1 dec si mov di,si mov cl,n2 mov si,offset content2 transfer2: sub byte ptr[si],30h inc si loop transfer2 dec si mov bx,si ;===================================== subbbb: mov si,offset buff4 mov dl,n1 cmp dl,n2 clc jg lowern2 mov cl,n1 mov dl,n2 ;=========n1<=n2================ subln1: mov al,[di] sbb al,[bx] aas mov [si],al inc si dec di dec bx loop subln1 pushf mov cl,n2 sub cl,n1 cmp cl,0 je ifequ popf ;========================= remain1n1: mov al,0 sbb al,[bx] aas mov [si],al inc si dec bx loop remain1n1 jmp done ;========================== lowern2: mov cl,n2 mov dl,n1 subln2: mov al,[di] sbb al,[bx] aas mov [si],al inc si dec di dec bx loop subln2 pushf mov cl,n1 sub cl,n2 remain1n2: mov al,0 sbb al,[di] aas mov [si],al inc si dec di loop remain1n2 ;============================= done: jnc over jmp refinement ;=========================== ifequ: popf jnc over ;=========================== refinement: mov al,0 sbb al,0 mov [si],al inc dl inc si ;=========================== over: dec si mov cl,dl mov dl,0ah mov ah,2 int 21h ;============================== transfer3: add byte ptr[si],30h mov dl,[si] mov ah,2 int 21h dec si loop transfer3 mov ah,4ch int 21h code ends end start ;============================ 该段代码的测试结果显示 当n1>n2时，且用0 填补空位，则输出结果正确 当n1<n2时，且用0 填补空位，则输出结果 例：n1=0123,n2=1234,结果显示为/8889，而8889=10000-（1234-0123） 且该规律始终不变 ```

<div class="post-text" itemprop="text"> <p>The following <a href="http://git.osmocom.org/rtl-sdr/tree/src/rtl_fm.c#n798" rel="nofollow">snippet of C code</a> is from <code>rtl_fm.c</code> which is part of the <a href="http://sdr.osmocom.org/trac/wiki/rtl-sdr" rel="nofollow">rtlsdr</a> project (I've added the <code>printf</code> statement)</p> <pre><code>for (i=0; i&lt;(int)len; i++) { s-&gt;buf16[i] = (int16_t)buf[i] - 127; } printf("buf %x %x, buf16 %x %x ", buf[0],buf[1], s-&gt;buf16[0], s-&gt;buf16[1]); </code></pre> <p>An example line of output is: <code>buf 7c 82, buf16 fffd 3</code></p> <p><code>buf16</code> is an array of type <em>int16_t</em>, <code>buf</code> is an array of bytes (char*), <code>len</code> is length of <code>buf</code></p> <p>I'd like to port this to Go. Here is what I've come up with: <a href="http://play.golang.org/p/zTRkjlz8Ll" rel="nofollow">http://play.golang.org/p/zTRkjlz8Ll</a> however it doesn't produce the correct output.</p> </div>

<div class="post-text" itemprop="text"> <p>I'm attempting to write a Go program to parse ans.1 BER two's complement integer encoding. However, the integer can have either 1, 2, 3 or 4 byte length encoding (depending on its size).</p> <p>According to the specification (<a href="http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf" rel="nofollow">http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf</a>) the leftmost bit is always the complement.</p> <p>What's a clean way to do this?</p> <pre><code>func ParseInt(b []byte) (int64, error) { switch len(b) { case 1: // this works return int64(b[0]&amp;0x7f) - int64(b[0]&amp;0x80), nil case 2: // left most byte of b[0] is -32768 case 3: // left most byte of b[0] is -8388608 case 4: // left most byte of b[0] is -2147483648 (and so on for 5, 6, 7, 8) case 5: case 6: case 7: case 8: default: return 0, errors.New("value does not fit in a int64") } } ParseInt([]byte{0xfe}) // should return (-2, nil) ParseInt([]byte{0xfe, 0xff}) // should return (-257, nil) ParseInt([]byte{0x01, 0x00}) // should return (256, nil) </code></pre> </div>

c语言中printf("%x",-1);为什么会输出-1的十六进制补码？？

c语言中printf("%x",-1);为什么会输出-1的十六进制补码？？？

“亚马逊丛林里的蝴蝶扇动几下翅膀就可能引起两周后美国德州的一次飓风……” 这句人人皆知的话最初用来描述非线性系统中微小参数的变化所引起的系统极大变化。 而在更长的时间尺度内，我们所生活的这个世界就是这样一个异常复杂的非线性系统…… 水泥、穹顶、透视——关于时间与技艺的蝴蝶效应 公元前3000年，古埃及人将尼罗河中挖出的泥浆与纳特龙盐湖中的矿物盐混合，再掺入煅烧石灰石制成的石灰，由此得来了人...

loonggg读完需要3分钟速读仅需 1 分钟大家好，我是你们的校长。我之前讲过，这年头，只要肯动脑，肯行动，程序员凭借自己的技术，赚钱的方式还是有很多种的。仅仅靠在公司出卖自己的劳动时...

MySQL数据库面试题（2020最新版）

HashMap底层实现原理，红黑树，B+树，B树的结构原理 Spring的AOP和IOC是什么？它们常见的使用场景有哪些？Spring事务，事务的属性，传播行为，数据库隔离级别 Spring和SpringMVC，MyBatis以及SpringBoot的注解分别有哪些？SpringMVC的工作原理，SpringBoot框架的优点，MyBatis框架的优点 SpringCould组件有哪些，他们...

《经典算法案例》01-08：如何使用质数设计扫雷（Minesweeper）游戏

《Oracle Java SE编程自学与面试指南》最佳学习路线图（2020最新版）