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
-1
votes
3 answers

bitwise operation and Two's complement in python

I have an array of (short) data which is shifted 4 to left and it is also a signed number. I need to plot it around zero. for instance: if the number in the array is 0b0000000011111111 and I shift it to left by 4, I will get 0b000000001111. it is…
-1
votes
1 answer

left shifting of a two's complement vector VHDL

I'm trying to solve some exercises, I have to shift a 8-bit vector named A into 2A (A+A). my soluction for this is: (A(7) and '1') & A(6 downto 0) & '0'; after this I made a two's complement of A in this way: entity complementare is port(a: in…
DarkPassenger
  • 67
  • 1
  • 8
-1
votes
1 answer

Why is this C function returning int values in two's complement?

I'm using a library that uses Intel's MMX single instruction, multiple data (SIMD) instruction set to speed up the multiplication of integer arrays. The function I am using contains in-line assembly to use the MMX SIMD registers in Intel processors…
Jake
  • 29
  • 1
  • 6
-1
votes
3 answers

Struct variable doesn't changed by assignment

struct st { int a1 : 3; int a2 : 2; int a3 : 1; } void main(void) { x.a3 = -1; if (x.a3 == -1) printf("TRUE\n"); else printf("FALSE\n"); x.a3 = 1; if (x.a3 == 1) printf("TRUE\n"); else…
BobKim
  • 9
-1
votes
1 answer

(C language) Is there a way to put conditional arguments for two different possible integer values in a while loop?

So lets say we have a function that can take either a positive or a negative integer binary string and count the amount of right shifts it takes to get to get to the leftmost significant bit I have something like this int shiftCount(int x){ int…
Team. Coco
  • 85
  • 2
  • 9
-1
votes
1 answer

Why does 0xff equal to negative 0 in C++

I saw some code looking like this: Object *area = new Object[n]; memset(area, 0xff, sizeof(Object) * count); So this fills the Object array's every field with bit 1. Then later there are multiple places checking if an object's field has not been…
Superziyi
  • 609
  • 1
  • 9
  • 30
-1
votes
2 answers

Finding n bit length 2s complement representation of a number

I am writing a function with parameters: int nbit2s(long int x, long int n){ } I am looking to take in 64 bit number x, and find if a 2 bit representation of n bit length is possible. I am however restricted to using only bitwise operators and…
-1
votes
1 answer

Writing a function to convert Two's Compliment to Base 10 in Python

I am tasked with writing a function that takes a string of arbitrary length of Binary in the Two's Compliment system, and return the corresponding integer in base 10. I can not use for or while loops, and I can't use built in converting functions. I…
-1
votes
2 answers

What is decimal value of the sum of the following 5-bit two's complement numbers?

Can someone explain this question? What is decimal value of the sum of the following 5-bit two's complement numbers? 10010+10101
-1
votes
2 answers

Is this code correct for 2's complement?

Homework time again. I have to create a program to print 1s and 2s complement of a binary number. So far is tis correct for 2s compliment? Should I allow input, then calculate 1's compliment before 2's? import java.util.Scanner; public class…
user2913004
  • 123
  • 1
  • 3
  • 8
-1
votes
2 answers

Twos complement enquiry

I'm currently doing twos complement in my course, using 8 bits I'm trying to complete this operation, its -85 + -44. I missed a lecture so I have tried to catch up on my own and this is what I have come up with, tell me if I'm right or wrong. -85…
Rapidz
  • 19
  • 5
-1
votes
5 answers

Converting from int to binary string to int using 2's complement

There is a binary string 10001110 which I know is using 2's complement. I know it will be 8 bits. It is given to me in the form of an unsigned int which equals 142. I then need to convert this back to 10001110 then invert all the bits and add one…
Tom Jenkinson
  • 1,390
  • 3
  • 19
  • 43
-2
votes
1 answer

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 number that's a bit too high, I'm doing 9 * -9 and…
-2
votes
2 answers

Why are the bits inverted in one's complement?

When storing a negative number with one's complement before you add the 1 for two's complement, why are all the bits other than the sign inverted? I suppose It would just be simpler if the only thing different was the sign. The only reason I can…
Greener
  • 1,327
  • 3
  • 13
  • 19
-2
votes
1 answer

MARS MIPS Bit Shifting using SRA

So when shifting -34 by 2 bits using SRA, I get an output of -9 using the code below. I can not figure out why it is giving me -9 and not another number addi $t0, $zero, -34 sra $s0, $t0, 2 addi $v0, $zero, 4 la $a0, result1 syscall addi $v0, $zero,…