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.

A two's complement number follows the same principal as a pure binary number, except for the first bit: the most significant bit. If this bit is 0, the binary number is positive, if it's 1 then the number will be negative.

For example:

01110 = 8 + 4 + 2
      = 14

10010 = -16 + 2
      = -14

So 01110 is 14 in denary and 10010 is -14 in denary

683 questions
2
votes
4 answers

Fail to Understand Java Two's Complement

I have this code: package com.company; import java.net.InetAddress; import java.net.UnknownHostException; public class Main { private final static int broadcast = 0xffffffff; //4294967295, or 255.255.255.255 private final static int…
Jon
  • 1,675
  • 26
  • 57
2
votes
2 answers

Two's Complement of a number and its negative number difference?

So i was told that two's complement is usually to used to find complement of a number and I used it only to complement positive numbers (i.e positve --> negative conversion) however I just got an example in book which asks me the following :…
Rage91
  • 87
  • 1
  • 9
2
votes
1 answer

Two's complement detect overflow with carries

If I add two signed binary numbers with Two's Complement method, why does it automatically mean that overflow has occoured if the carry into the MSB (the sign) and the carry out are not the same?
Nic
  • 21
  • 2
2
votes
1 answer

Two's Complement: Carry and Overflow

I think I know what a carry bit is and I also understand the concept of overflow. However, I fail to identify these when performing a simple addition/subtraction using two's complement. Therefore I performed some calculations and expressed three…
typeduke
  • 6,494
  • 6
  • 25
  • 34
2
votes
3 answers

What is the preferred way to take the modulus by a power of 2 of an int in Python

Imagine that you have some counter or other data element that needs to be stored in a field of a binary protocol. The field naturally has some fixed number n of bits and the protocol specifies that you should store the n least significant bits of…
Feuermurmel
  • 9,490
  • 10
  • 60
  • 90
2
votes
1 answer

Subtraction of binary fractions using 2's complement

I have been through different posts here in Stack Overflow relating my question, but none seem to answer it because in their questions, either the decimal representation is given (mine isn't) or the answer is vague to me (like this). I'm trying to…
ellekaie
  • 337
  • 1
  • 7
  • 21
2
votes
1 answer

1's complement to 2's complement conversion

My code is a class for converting decimal to binary. it will display the binary, one's complement and two's complement. So far I have figured out the one's complement but having a problem finding a solution for the two's complement. my plan was to…
tms777
  • 35
  • 1
  • 5
2
votes
1 answer

Two's Complement -- How are negative numbers handled?

It is my understanding that numbers are negated using the two's compliment, which to my understanding is: !num + 1. So my question is does this mean that, for variable 'foo'=1, a negated 'foo' will be the exactly the same as variable 'bar'=255. f…
user2958652
  • 136
  • 1
  • 7
2
votes
2 answers

Assembly MASM Dealing with Negative Integers

I was instructed to write a program in assembly that will carry out the following arithmetic: ((A + B) / C) * ((D - A) + E) I've succeeded in doing this when no negative values come into to play, but suppose A = 5, B = 4, C = 3, D = 2, and E = 1.…
user2975482
  • 21
  • 1
  • 2
2
votes
3 answers

How to uniformly detect an integer’s sign bit across various encodings (1's complement, 2's complement, sign magnitude)?

How to detect an int sign-ness in C? This question is mostly of historical machines. What I am asking how to distinguish if an integer is 0 or -0. In 1's complement and sign/magnitude int encoding, both a 0 (or +0) and -0 are possible. The simple…
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
2
votes
2 answers

Are python int's architecture specific?

We can define variables as integer values, e.g. x = 3 y = -2 and then operate on bits with binary operators &, |, ^ and ~. The question is if we always get the same result on every architecture, or is the behavior architecture specific? Can we…
Miguel
  • 658
  • 1
  • 6
  • 19
2
votes
2 answers

Converting Decimal to Two's-Complement

Instructions: Convert these decimal numbers to 5-bit 2's-complement form, if possible. If not possible, explain why this is so. (16) base 10 According to the online converter: From what I understand, If decimal is positive: STEP 1: Convert…
k_rollo
  • 5,304
  • 16
  • 63
  • 95
2
votes
3 answers

Algorithm to write two's complement integer in memory portably

Say I have the following: int32 a = ...; // value of variable irrelevant; can be negative unsigned char *buf = malloc(4); /* assuming octet bytes, this is just big enough to hold an int32 */ Is there an efficient and…
Ricky Stewart
  • 1,102
  • 1
  • 8
  • 18
2
votes
4 answers

overflow during operation a+(-a)

Asumming a=1 1+(-1) =0 but with 4 bit binary ,using 2's complement 0001+1111=10000 ~ 0000 Isn't signed integer overflow undefined behavior ? Do we depend on undefined behavior for such trivial result or I am missing something . from wikipedia: If…
Zxcv Mnb
  • 733
  • 8
  • 19
2
votes
1 answer

Branch-on-condition instruction of the 6502 with signed bytes?

I'm making a 6502 emulator (and after that I'll emulate the other NES components around it to have a fully functional NES emulator) and I came across the branch-on-condition instruction (relative). Now, what I'm wondering about is, is the byte…
ZimZim
  • 3,291
  • 10
  • 49
  • 67