/* 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;
}
1
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.)
87
Yes
It is a simple linear equation in 's'. Its solution is the number that 's' must bein order to make it a true statement. The solution may be found like this:4s + 10 = 2s + 2Subtract 10 from each side of the equation:4s = 2s - 8Subtract 2s from each side:2s = -8Divide each side by 2 :s = -4
1
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 � 0001 0011 1s � 1110 1100 2s � 1110 1100 + 1 = 1110 1101
-15 is 11111111 and 2s com is 1111 0001
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.
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.)
-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 => 1, any 1 => 0): 00 0111 1011 => 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.]
Yes
87
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.
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.
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 → 1111 0101 = 245 in decimal 16 bits: decimal 10 = 0000 0000 0000 1010 → 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.
Binary numbers, with or without a computer are a series of 1's and 0's.