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

What is wrong with my Ones Complement?

i want to do the following subtraction using ones complement Octal(24)-Hex(4B) and give a binary answer Octal(24) is 20 decimal and Hex(4B) is 75 in decimal 20->10100 75->1001011 taking 1s complement of 75 0110100 and adding to…
0
votes
3 answers

converting signed 255 into 8-bit twos complement

I need to convert a signed decimal number (255) into 8-bit two's complement for a HW problem. My problem is that I'm not sure how to make a number like 255 positive with 8-bits, because I believe the range is -128 to 127, correct? If 01111111 is…
user2713971
  • 23
  • 2
  • 11
0
votes
5 answers

Two's Complement Value of 011101

I know the answer is 29 but I'm not sure how to arrive at it. Usually I would take 011101 get its inverse of 100010 and add 1 to get 100011. The value of this is 35. How then is the answer 29?
user1766888
  • 417
  • 1
  • 8
  • 21
0
votes
1 answer

Two's complement operation with hexadecimal

Im having trouble understand a portion of my textbook on learning assembly language. Its beginning to introduce all ways to represent data in a computer, and im at the point where it covers signed and unsigned numbers in a computer, where signed…
Code Doggo
  • 2,146
  • 6
  • 33
  • 58
0
votes
3 answers

What is 2's Complement Number?

What is 2's Complement Number? Why do we take 1's Complement and add 1 to it? Why don't we subtract 1 after taking 1's Complement? Why do computers use 2's Complement?
0
votes
5 answers

Printing to output: integer as sum of powers of 2

I had an exam, and I've been struggling ever since. You have an array of integers(ex. 13, 6, 21, 4), and I need to make an output that looks like: 13 = 2^3 + 2^2 + 2^0 6 = 2^2 + 2^1 21 = 2^4 + 2^2 + 2^0 4 = 2^2 here's what i've got so far.…
Cola GGS
  • 3
  • 3
0
votes
1 answer

Signed integer to two's complement hexadecimal

I have written a function in Python to take a signed integer, convert it using two's Complement then return the hexadecimal value. I know there is a hex() function, but I want to be able to specify the size of the integer. How could I improve the…
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0
votes
1 answer

Truncation in Two's Complement?

I'm struggling to understand how truncation works when converting from unsigned to Two's Complement. Can someone please explain? (my textook uses the example of truncating a 4 bit value to a 3 bit value, and says that -1 becomes -1, but -5 becomes…
girlrockingguna
  • 291
  • 1
  • 3
  • 13
0
votes
3 answers

bitwise shift operator under different platforms (windows, mac os, android)

I am debuging a function hashKey. The problem is that it generates different result for the same input under different platforms, windows/win ce, mac os, android. Here is the code: unsigned long hashKey(const char *name,size_t len) { …
0
votes
1 answer

Understanding Two's Complement Addition

I've built a four bit adder/subtractor utilizing 4, 1 bit full adders and the input and output are twos complement numbers. If X=0111 and Y=1000 their sum is obviously 1111. In decimal this is equivalent to 7 + 8 thus 15 which is what the sum…
ZAX
  • 968
  • 3
  • 21
  • 49
0
votes
1 answer

C# convert one's complement bits to a two's complement long?

In C#, how can I convert a 64 bit ones complement number (represented as a long or ulong) to a signed two's complement long? For reference, I'm trying to implement ULP-based double comparison using BitConverter.DoubleToInt64Bits().
ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152
0
votes
1 answer

How can I check a negative solution of a twos complement subtraction equation?

Hey guys I'm still trying to get the hang of twos complement arithmetic and I can get the correct answer, since I'm working on practice problems with solutions. When I take the answer that's in binary, I can't seem to equate it out to the decimal…
ModdedLife
  • 669
  • 2
  • 11
  • 24
0
votes
3 answers

Are negative numbers that are left shifted *always* filled in with "1" instead of "0"?

I'm independently studying bit-shifting by porting some C++ functions to .NET's BigInteger. I noticed that when I shifted BigInteger the void was filled in with ones. I believe this has to do with the negative number being stored in twos…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
0
votes
3 answers

Trouble with a Two's Complement Function

Trying to implement a function to return the two's complement of a string of bits. I've tried two varieties and get odd results. Version 1 (does the inversion but not the "+1"): string twosComp(signed int number) { string twosComp(signed int number)…
frankV
  • 5,353
  • 8
  • 33
  • 46
0
votes
4 answers

Why does BitConverter shrink my already allocated array? (I'm trying to prevent a two's complement issue)

I am allocating an array that is intentionally bigger than the result of BitConverter.GetBytes. My goal is to leave the last byte empty so that I can prevent this number from being seen as the two's compliment and have tempPosBytes2[ When I run…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448