answersLogoWhite

0


Best Answer

/* note that neither of these functions have been tested, so there may be typos and the like */

/* if you're looking to return a real value: */
unsigned int complement(unsigned int value){
unsigned int returnvalue = 0;
while(value){

returnvalue <<= 1;
returnvalue += !(value & 1);
value >>= 1;

}
return returnvalue;

}

/* if you're looking for a string representing the binary number: */
char *complement(unsigned int value){
int numchars = 8 * sizeof(unsigned int);
int n;
char *returnvalue = malloc((numchars + 1) * sizeof(char));

for(n = 0; n < numchars; n++){
if(value & (1 << (numchars - 1 - n))){
returnvalue[n] = '1';

}else{
returnvalue[n] = '0';

}

}
returnvalue[numchars] = (char)NULL;

return returnvalue;

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: 2's complement of a binary number in c language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Why 2's complement binary subtraction is preffered over 1's complement binary subtraction?

1


What is the binary complement 8 bit representation for negative 19?

8-bit 2s complement representation of -19 is 11101101 For 1s complement invert all the bits. For 2s complement add 1 to the 1s complement: With 8-bits: 19 &acirc;&#65533;&#65533; 0001 0011 1s &acirc;&#65533;&#65533; 1110 1100 2s &acirc;&#65533;&#65533; 1110 1100 + 1 = 1110 1101


What is binary equivalent of -15 using 2's complement?

-15 is 11111111 and 2s com is 1111 0001


The 2's compliment of the number 1000 is?

To get the 2s complement, find the 1s complement (by inverting the bits) and add 1. Assuming that number is [4-bit] binary it would be 1000. If it is preceded by 0s, as in, for example, 0000 1000, then it would be 1111 1000.


What is the integer equivalent represented by 2's complement of the binary number 10001011?

To get the 2s complement, change all 1 bits to 0s and all 0 bits to 1s, and add 1 to the result. So the 2s complement of the 8-bit binary number 10001011 is the binary integer 01110101. If you want that in decimal, then remember that each place value column is twice the value of the place value column to its right, and the rightmost place value column for an integer is 1. Thus 01110101 in decimal is 64 + 32 + 16 + 4 + 1 = 117 (And 10001011 as a signed 8-bit binary integer represents the decimal integer -117.)


Do binary numbers consist of 1 and 2s?

Yes


How do you represent -123 in twos complement in 10 bits register?

-123 = 11 1000 0101 [I presume the number 123 is in decimal] First write the positive number in binary using 10 bits (I've split it into groups of 4 bits to make it easier to read): 123 = 00 0111 1011 Convert to 1s complement by inverting all bits (any 0 =&gt; 1, any 1 =&gt; 0): 00 0111 1011 =&gt; 11 1000 0100 Finally add 1 to get 2s complement: 11 1000 0100 + 1 = 11 1000 0101 Thus the 2s complement of the negative number: -123 = 11 1000 0101 This can also be expressed in hexadecimal: -123 = 0x385 Or in octal (easy to convert if the binary number is first written in groups of 3 bits): -123 = 1 110 000 101 = 01605 [I've used C notation for the hexadecimal and octal numbers.]


What is the 2's complement value of -85?

87


How you do find the binary code of -19?

One way is using the Twos (2s) complement; that is you find the binary representation of the positive number of -19, flip all the bits (0s to 1s and 1s to 0s), and finally add 1 to it.The positive bit of -19 is simply 19. It has a binary code of 0001 0011 (8 bits are required for this particular method). Flipping the bits, we get 1110 1100. Adding 1 (or 0000 0001) to our last answer, we get 1110 1101 which would be the binary representation of -19.


How do you detect overflow when adding two numbers in 2s complement form?

You can detect overflow if the result turns out to be negative (which is the same as checking to see if the sign bit is 1). For example if you tried to add 5 and 6 in to 4-bit 2s complement, you would get 0101 + 0110 = 1011, which is a negative number since the sign bit (the 1 on the left) is a 1. This is an overflow.


What is the 1's complement of 10?

The 1's complement is formed by inverting every binary digit (bit) of the number - if it is a 0 it becomes a 1, otherwise it is a 1 and becomes a 0. If 10 is in base 2, then its 1's compliment is 01 or just 1. If 10 is in base 10, then in binary it is 1010 and its 1's complement is 0101 = 5 in decimal. However, if more bits are being used to store it, there would be leading 0s that get inverted to 1s and so the resultant number is different; examples: 8 bits (a byte): decimal 10 = 0000 1010 &rarr; 1111 0101 = 245 in decimal 16 bits: decimal 10 = 0000 0000 0000 1010 &rarr; 1111 1111 1111 0101 = 65525 Next, if 2s complement is being used to represent negative numbers, the binary 1111 0101 represents decimal -11; similarly 1111 1111 1111 0101 represents decimal -11.


Computers process binary numbers which are composed of?

Binary numbers, with or without a computer are a series of 1's and 0's.