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
4
votes
2 answers

Negative integer to binary in Golang

How can a negative integer be represented as a binary in Go, using the two's complement notation? For example: n := int64(-1) fmt.Printf("%b", n) fmt.Println(strconv.FormatInt(n, 2)) Both lines print -1. The result shoud be like ffffffffffffffff.
Radoslav Stoyanov
  • 1,460
  • 5
  • 27
  • 40
4
votes
2 answers

C - three bytes into one signed int

I have a sensor which gives its output in three bytes. I read it like this: unsigned char byte0,byte1,byte2; byte0=readRegister(0x25); byte1=readRegister(0x26); byte2=readRegister(0x27); Now I want these three bytes merged into one number: int…
muliku
  • 416
  • 3
  • 17
4
votes
4 answers

How to get 2's complement of a binary number in Java programmatically

How to calculate the 2's Complement of a Hex number in Android/Java. For Example : String x = 10011010; 1's complement of x = 01100101; 2's complement is 01100110; How I can pro-grammatically achieve in Java? I had tried the…
Sarbjit Singh
  • 197
  • 1
  • 3
  • 13
4
votes
1 answer

Whats the highest and the lowest integer for representing signed numbers in two's complement in 5 bits?

I understand how binary works and I can calculate binary to decimal, but I'm lost around signed numbers. I have found a calculator that does the conversion. But I'm not sure how to find the maximum and the minumum number or convert if a binary…
Nooblhu
  • 552
  • 15
  • 33
4
votes
3 answers

Does RISC-V mandate two's complement or one's complement signedness, or is it implementation-determined?

I have looked through the ISA spec and searched the internet for the answer to this, but I could not find it. In the RISC-V ISA, should negative numbers be represented with one's complement or two's complement? Or, is this decision left to…
M. Moo
  • 74
  • 1
  • 6
4
votes
3 answers

How does imul and idiv really work 8086?

I am trying to figure out how the imul and idiv instructions of the 8086 microprocessor work. I know this: 1. mul and div are multiplications and division for unsigned numbers 2. imul and idiv, are also multiplications and divisions but for signed…
user1812076
  • 269
  • 5
  • 21
4
votes
1 answer

Is the non-negative range of a signed C++ integer at least as big as the negative range?

Does the C++ standard mandate that the non-negative range of a standard signed integer type is at least as big as the negative range? EDIT: Please note that I am referring to the non-negative range here, not the positive range which is obviously one…
Kristian Spangsege
  • 2,903
  • 1
  • 20
  • 43
4
votes
0 answers

How do I cast a number to BouncyCastle.BigInteger <--> System.Numerics.BigInteger

The slowest part of my code is that I use Bouncy Castle's "BigInteger.Multiply()" method. To try and speed things up I'm attempting to insert the code below to see if there is any improvement. I know that I'm not doing something right, because…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
4
votes
3 answers

longitude reading measured in degrees with a 1x10^-7 degree lsb, signed 2’s complement

I am receiving data from a gps unit via a udp packet. Lat/Lng values are in hex. Example Data 13BF71A8 = Latitude (33.1313576) BA18A506 = Longitude (-117.2790010) The documentation explains that longitude/latitude readings are measured in…
brady321
  • 1,475
  • 13
  • 14
4
votes
0 answers

How to divide two negative numbers using the two's complement method?

I was studying more about binary arithmetics and came across the problem -21/-3. The expected answer is 7. Of course I know we can do it by canceling the negative sign and dividing 21 by 3. I have seen the beauty of two's complement in the…
Nithin Jose
  • 1,029
  • 4
  • 16
  • 31
4
votes
1 answer

Two's complement number representation

I've recently been implementing a specialized parser for a slightly modified Abstract Syntax Notation. The specification says that integers are encoded as an array of octets which are to be interpreted as a binary two's-complement integer. So, at…
Channel72
  • 24,139
  • 32
  • 108
  • 180
4
votes
2 answers

Is two's complementary representation universally platform-independent?

it's idiomatic to initialize a block of memory to zero by memset(p, 0, size_of_p); when we want to initialize it to minus one, we can: memset(p, -1, size_of_p); no matter what type p is, because in two's complemenatry representation, minus one is…
Kun Wu
  • 329
  • 2
  • 8
4
votes
3 answers

Calculate two's complement checksum of hexadecimal string

I have a string "0AAE0000463130004144430000" and I need to calculate the two's complement checksum of the hex bytes that make up the string. The formula for the example string above is Sum the values: 0A + AE + 00 + 00 + 46 + 31 + 30 + 00 + 41 +…
Crake
  • 1,661
  • 1
  • 13
  • 30
4
votes
4 answers

How to read 2’s complement value from two registers into an int

I am trying to read values from the STC3100 battery monitor IC, but the values I am getting are not correct. What the datasheet says: The temperature value is coded in 2’s complement format, and the LSB value is 0.125° C. REG_TEMPERATURE_LOW,…
Reto
  • 1,660
  • 3
  • 19
  • 31
3
votes
2 answers

Converting between a 3-byte slice and signed integer type

I have a slice consisting of 3 bytes (ordered in LE) that is representing a signed integer, and I want to convert it to any of the integer types, preferably int32, and then back to itself. b := []byte{0x01, 0x00, 0x80} I tried to do that using…
Roj
  • 995
  • 1
  • 8
  • 22