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
1 answer

Why does gcc not add tmin + tmin correctly?

I've been playing around with bitwise operations and two's complement, when I discovered this oddity. #include int main () { int tmin = 0x80000000; printf("tmin + tmin: 0x%x\n", tmin + tmin); printf("!(tmin + tmin): 0x%x\n",…
Rushabh Mehta
  • 1,529
  • 1
  • 13
  • 29
-2
votes
1 answer

Two's Compliment - Method Analysis

I have a question about the method of two's compliment: Which of these two methods are correct, and how can I disprove the incorrect one? Starting example task: Convert the negative denary number -20 to two's compliment negative binary number (which…
-2
votes
3 answers

byte a=123; byte b=5 byte c= (byte)(a+b); gives -128. Why does this really happen?

https://www.ideone.com/gYreaO , why does this really happen? Why does it take the two's complement way to display the value? byte a=123; byte b=5; byte c=(byte)(a+b); System.out.println(c);
Karma
  • 9
  • 1
-2
votes
4 answers

C find maximum two's complement integer

I am tasked with finding maximum two's complement integer, or the TMax. I am at a complete loss for how to do this. I know that the correct value is 0x7fffffff, or 2147483647, but I do not know how exactly to get to this result. That's the maximum…
Sam Tyson
  • 71
  • 2
  • 9
-2
votes
1 answer

2'complement for negative numbers

Say we are having 4 bits to represent a SIGNED number so, a total of 2^4 = 16 numbers (using 2's complement method). the range is from -8 to +7. The range, i see ,is from -7 to +7. if the first bit is all about the sign how come it represent a…
prabesh013
  • 15
  • 2
-2
votes
3 answers

Seven bit and two compliment

If we use seven-bit two's complement binary representation for integers, what is The number of integers (things) that can be represented in this way? The smallest (most) negative integer that can be represented in this way? The largest positive…
-2
votes
1 answer

strange behavior of & and ^ in python 2.7

Here are my code and wondering why different results in both cases? Actually I expect first line output -1, since bit AND 0xffffffff does not change anything. And I expect second line output 1, since bit XOR 0xffffffff means bit NOT (which is ~ in…
Lin Ma
  • 9,739
  • 32
  • 105
  • 175
-2
votes
2 answers

Finding if a value falls within a range, using bitwise operators in C

So i am working on this method, but am restricted to using ONLY these operators: <<, >>, !, ~, &, ^, |, + I need to find if a given int parameter can be represented using 2's complement arithmetic in a given amount of bits. Here is what I have so…
-2
votes
1 answer

Floating point to 16 bit Twos Complement Binary, Python

so I think questions like this have been asked before but I'm having quite a bit of trouble getting this implemented. I'm dealing with CSV files that contain floating points between -1 and 1. All of these floating points have to be converted to 16…
likeabbas
  • 115
  • 1
  • 10
-2
votes
3 answers

How to convert between floats and decimal twos complement numbers in Python?

I want to convert decimal float numbers in python into twos complement decimal binary numbers. For example 1.5 in twos complement decimal 2.6 (8 bits) would be 0b011000. Is there a module that can do this for me?
Chet
  • 18,421
  • 15
  • 69
  • 113
-3
votes
2 answers

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 18446744073709551615 currently, my understanding is the…
-3
votes
1 answer

Convert a negative number with a fraction in 2's complement

I want to convert number -0.25. So 0.25 (for 4 bit int and 4 bit frac) equals to: 0000_0100 For negative number -1 answer is 1111 But what is -0.25? How can I convert this negative number with a fraction in 2's complement?
Nivensy
  • 1
  • 3
-3
votes
1 answer

read value of binary two's complement integer in c++

I have a binary data file. There is a binary header of a few thousand bytes. I task is to read bytes 3000 and 3001. The good news is that it should read one of two values: 1 or 5. The hard part for me is that the number is stored as a two's…
-3
votes
1 answer

calculate the difference between two binary numbers with a logic circuit

I wanted design a logic circuit that calculates the difference between two binary numbers with sign A and B with 4 bit and also would like to calculate propagation delay and determine the value?? could anyone help! I did make a xor gate with a…
anil cheetri
  • 1
  • 1
  • 2
-3
votes
3 answers

Two's complement and loss of information in C

I want do the two's complement of a float data. unsigned long Temperature ; Temperature = (~(unsigned long)(564.48))+1; But the problem is that the cast loses information, 564 instead of 564.48. Can i do the two's complement without a loss…
physics
  • 161
  • 9
1 2 3
45
46