Questions tagged [twos-complement]
Two's complement is a mathematical operation on binary numbers, as well as a binary signed number representation based on this operation.
656
questions
-1
votes
1
answer
46
views
How to subtract negative numbers from negative numbers using Complement notation?
I've just started learning. I am trying to understand this thoroughly and deeply. I somewhat understand it as long as it's subtraction between a Smaller number from a Larger number or a Larger number ...
0
votes
1
answer
22
views
Reverse binary reading
I have a document that shows a data in HEX, and split it into multi part (head, TransactionID, MessageType and ...)
There is a problem in this document, and it is for example for TID they write ...
0
votes
0
answers
23
views
Why add 1 when converting from 2's complement back to decimal?
When we want to show 8-bit binary representations of a negative number -123 in two’s complement we do:
Number in decimal = -123
Convert -123 to its absolute value: 123
Covert to binary: 01111011
One'...
0
votes
1
answer
21
views
Need some assistance understanding binary addition/subtraction using 2's complement
If A = 01110011, B = 10010100, how would I add these?
I did this:
i.e: 01110011 + 10010100 = 100000111
Though, isn't it essentially 115 + (-108) = 7, whereas, I'm getting -249
Edit: I see that ...
0
votes
1
answer
26
views
Reading internal temperature of an ADC that returns 3 databytes in two's complement (micropython)
As the title suggests, I'm trying to read the internal temperature of an ADC (The ADS1235 from Texas Instrument to be precise) using the raspberry Pi Pico running micropython.
The SPI communication ...
-1
votes
1
answer
52
views
How does 0FFFFh equal -1?
mov ax, 0ffffh
inc ax
inc ax
Was watching a video on basic Intel X86 Assembly.
I thought 0FFFFH was 65535 but in the video they got -1 instead (before the inc instructions run). Just wondering how ...
0
votes
1
answer
36
views
How can we get the encoding formula of two's completment from its definition
I read the wiki article and many other articles.
The definition in wiki is that "The defining property of being a complement to a number with respect to 2N is simply that the summation of this ...
0
votes
2
answers
95
views
Converting sensor data (two's complement) to signed integer
I've seen a couple old posts that had a similar context but the answers were code snippets, not explanatory. And I can't even get the code offered in those answers to compile.
I apologize in advance ...
0
votes
1
answer
39
views
convert a filename eg file01010101 from decimal to binary using two's complement, without using built in binary function in bash
This is the binary evaluation and conversion part of the shell script.
elif [[ $file == file[0-1][0-1][0-1][0-1][0-1][0-1][0-1][0-1] ]]
then
first=`echo $file | cut -c 5`
second=`echo $file | ...
0
votes
0
answers
5
views
2's Complement - Another Interpretation. Issues?
I've been browsing around the internet looking to validate my interpretation of 2's Complement.
Everywhere I look (including my educational class) I see the following:
Invert all bits in the # (1->...
2
votes
2
answers
91
views
How Can I Convert Hex 'F933F177' to Decimal -114.036361 via SnowSQL or a Java function?
I have coordinates stored in HEX, which from searching online appear to have used Signed 2's Complement to handle the negative values. I'm getting a bit lost with where the various conversions are ...
0
votes
2
answers
40
views
Weird Lazy Casting Behaviour for character in Java 11
Can someone kindly explain the casting behaviour I illustrated below?
Code
char charo = (char) -1;
System.out.println(charo);
System.out.println((short) charo);
System.out.println((int) charo);
...
0
votes
1
answer
49
views
Why the number is not negative when we have 1 in MSB in subtraction of hexadecimal numbers
I'm learning about addition/subtraction in Assembly. I searched for articles to see how to know if a hexadecimal number is negative/positive and saw this page below, from this site
Subtraction part
My ...
0
votes
2
answers
107
views
Why does ^1 equal -2?
fmt.Println(^1)
Why does this print -2?
0
votes
1
answer
38
views
Does an ADC with bipolar input has more resolution on the negative "part"?
I'm trying to understand if the negative part of a bipolar ADC has more resolution than the positive.
For simplicity, let's say I have a 3 bit ADC, for a total of 8 possible values. Let's also say ...
0
votes
0
answers
68
views
Twos complement in MASM
why is it that in assembly language that if I subtract a variables value from a register that has a value of zero that I end up getting the two’s complement of that variable’s value?
If I have an var1 ...
0
votes
0
answers
17
views
Are the binary forms of signed integers and two's complement different?
After learning about signed integers and two's complement in lecture, I am confused about the difference between the two, if there is any.
For example, would -1 signed integer in binary form be ...
-1
votes
3
answers
64
views
Char addition does not have expected result in C
Can someone explain me how is this at the end
a=?,b=?,c=-124
and not
a=?,b=?,c=132
This is code:
#include <stdio.h>
int main() {
char a = 'D', b='C', c='A';
a = c + 'D';
b = a + b + 'A'...
-4
votes
1
answer
135
views
How does sign contraction work from 16 bit to 8 bit?
How do I sign contract ff12.here it is a negative number but I need to remove all the FF here but if I do that it becomes a positive number.
One definition of "sign contraction" can be ...
0
votes
1
answer
49
views
Why complement of 0 is -1? [duplicate]
I was learning about bitwise operators and I learnt that complement of 0 is 1 and 1 is 0. But when I tried using ~0 on IDLE, it printed -1 and when i typed ~1 it gave -2..
0
votes
0
answers
21
views
Two's Complement of a Fractionary Binary Number
Two's Complement of 1101 = 0011 (invert all the bits and add 1 to the right-most bit)
I know how to apply Two's Complement to whole numbers, I just don't know how to do it to fractionary ones.
For ...
0
votes
1
answer
91
views
Two's Complement of fractional numbers
I've been searching around the web for a solution for this question for several weeks but without any success.
My University professor told me that to convert a binary number with a fractional part to ...
0
votes
0
answers
42
views
Two's complement- Calculate minmal number of bits needed for result
let's say we have two decimal numbers:
-15 = 10001 in two's complement, 5 bits
-9 = 10111 in two's complement, 5 bits
Now, result of addition of those two numbers is -24 = 101000 in two's complement,...
0
votes
2
answers
45
views
Is there a relation between these two operations?
I've noticed that when converting to and from 2's complement, we can either add or subtract one, depending on when we perform the bitwise complement.
i.e., let P be an integer, to represent -P we ...
1
vote
1
answer
142
views
What does -1 in the solution of this code mean? Why is xor with -1 equivalent to a bitwise invert / not?
Provide a minimal set of RISC-V instructions that may be used to implement the following pseudoinstruction:
not x5, x6 // bit-wise invert
solution:
xori x5, x6, -1
0
votes
1
answer
107
views
Convert positive number e.g. +7 to twos complement in 8 bits showing a leading 1 using 4 step method
For the purposes of teaching, I wish to understand the following in the context of the method (four steps, see below): I am trying to convert +7 to twos complement in 8 bits. The answer, as per shown ...
0
votes
1
answer
129
views
Sum of 2 two's complement binary number
I am familiar with two's complement when performing addition in 4 bits, then I am confused when I face the question below
** Find the sum of the 2 two's complement binary number 010111 and 110101 in 8 ...
-1
votes
1
answer
223
views
How to multiply by -1 without using `MUL` or `NEG`
I got an assignment to write a program in assembly that can multiply a number by -1 without using the neg or mul instructions. I tried to use shl and shr but I can't make it work for some reason. Does ...
-2
votes
1
answer
33
views
Way to get the twos compliment decimal from binary?
I have a simple question, how do I get the twos compliment instead of the regular answer from binary? I have a program that does the booths algorithm but when it returns the decimal it gives me a ...
1
vote
1
answer
70
views
A bitwise shortcut for calculating the signed result of `(x - y) / z`, given unsigned operands
I'm looking for a neat way (most likely, a "bitwise shortcut") for calculating the signed value of the expression (x - y) / z, given unsigned operands x, y and z.
Here is a "kinda real ...
0
votes
1
answer
77
views
How to accurately emulate `int32`, signed 2's complement 32-bit integers, in python
Python supports arbitrary bit-length integers, but I would like to emulate int32, 32-bit integers, in all their overflow glory.
I have few questions and comments about this
int32 has INT32_MIN = -(1 &...
2
votes
0
answers
128
views
Assembly movsbq instruction [duplicate]
I understood the difference between movz and movs.
movz fills the empty bytes with zeros while movs fills them with sign bit (zeros or ones).
However, I came accross the following example
movabsq $...
0
votes
1
answer
23
views
Which arithmetic properties do two's complement integers have?
There are multiple arithmetic properties that hold for Whole Numbers, the ones we're used to when performing calculations:
'+' Commutativity: a+b == b+a
'+' Associativity: (a+b)+c == a+(b+c)
'*' ...
1
vote
1
answer
39
views
Can integer division yield a different result when applied on unsigned operands vs signed operands?
I'm asking, of course, in terms of the actual "bit data" returned.
In other words, can the following function ever return false:
bool func(uint x, uint y)
{
return x / y == (uint)((int)x ...
0
votes
1
answer
155
views
FInd Two's Complement for Unsigned Integer
I have a simple function that will find the two's Complement for an unsigned integer and then test it to make sure that it is correct
unsigned twoscomplement(unsigned v) {
};
int main()
{
...
0
votes
1
answer
139
views
Two's Complement Division Algorithm (Intel)
On Intel machines, integer division requires a sequence of two instructions:
After you store the dividend in the a register (e.g., %eax), you extend the sign of the dividend into the corresponding d ...
2
votes
1
answer
159
views
Why is MSB used as the sign bit?
I was reading Kip Irvine's book on x86 assembly programming when in the explanation of signed integers, it was mentioned that the MSB (Most Significant Bit) is used as the signed bit, 0 for +ve and 1 ...
0
votes
2
answers
66
views
Trying to perform bitwise operation on a negative number to make 0s in bits 31-16 (32 bit number) in C
I have a number, -3, that I would like to use as a 16 bit integer and use an | (or) operator with a 32 bit integer.
So ideally, I need it so that -3, in binary two's complement, will be 0000 0000 0000 ...
0
votes
0
answers
102
views
Binary Representation (Signed Magnitude, 1's Complement, 2's Complement)
I am new to binary representation and would like to clarify some questions...
From my understanding:
The first bit of Signed Magnitude represents the sign (0 = Positive, 1 = Negative)
1's complement ...
0
votes
2
answers
493
views
4-bit Two's complement overflow
I have a conceptual (ish) question that I can't understand. I have an assignment and it is telling me to choose which of the following 4-bit binary (2's complement) additions will result in overflow. ...
0
votes
3
answers
193
views
Binary 12 is 1100 but signed 4 is also 1100. Can someone please explain how this works?
i know that we represent negative number by using 2's complement but some of the binary value that we get after 2's complement are having the same binary number. so, how do this works?
1
vote
1
answer
146
views
Wrong result when subtracting a negative number (therefore performing an addition actually) in Verilog
I'm learning Verilog and, to do so, I have been told to implement a fixed-point, signed number calculator, for operations +,-,* and /. The format I have chosen for these numbers is SQ4.4; in other ...
-3
votes
2
answers
177
views
uint(0) - uint(1) output maxValue of uint64
Can anybody tell me why the output is not -1 but 18446744073709551615?
package main
import (
"fmt"
)
func main() {
a := uint(0)
b := uint(1)
fmt.Println(a - b)
}
output
...
0
votes
1
answer
367
views
How to use bit-level operations to convert single-precision floating-point numbers to integers
I am doing a data lab of CMU.
Here is the question
/*
* floatFloat2Int - Return bit-level equivalent of expression (int) f
* for floating point argument f.
* Argument is passed as unsigned int,...
0
votes
0
answers
245
views
In binary subtraction, how do you handle a borrow when there are no bits left to borrow form
Assume we use 3 bits to subtract 1 from 0:
0b000
- 0b001
------
0b111
I have the following 2 questions:
1- Where is the borrowed 1 into the MSB coming from? When we do 0b000 - 0b001, the MSB of ...
0
votes
2
answers
552
views
How is overflow detected when doing binary subtraction
Assume we have 3 bits to play with. I am going to represent plus and minus 3 in 2's complement:
+3 = 0b011
-3 = 0b101
When doing addition you always have a dangling bit when an overflow happens like ...
0
votes
0
answers
25
views
Unpack 3 and 6 byte two's complement integer in Python [duplicate]
How can I convert 3 and 6 byte big-endian two's complement Python bytes instances to Python ints?
I was hoping to use struct, but looking at https://docs.python.org/3/library/struct.html#format-...
1
vote
2
answers
196
views
What are widening integer operations?
Let's examine Add and Multiply as examples. Which one can be categorized as widening?
Assume inputs are signed characters (i.e 8 bits in length) in 2's complement, unless declared otherwise.
Positive ...
0
votes
2
answers
182
views
Two's complement giving different answer in java
Sorry if my question is basic or duplicate of another question. Anyway, I am trying to perform two's complement to change the sign of a number and to check the result in java but the result is ...
0
votes
3
answers
254
views
Which complement does C use internally?
Everything I look up just tells me how to do complement operations/calculations in C.
I want to know what representation does C use internally and how does it handle with overflow.