I have used the 2's compliment approach to convert a negative number to binary I am getting the answer right
after I converted the number to binary let's say n = -6
- I ignored the negative sign (by making the number positive)
- I took it's 2's compliment
Now if the MSB (Most Significant Bit) is 1 that means the number is negative this ans is stored in newAns
But my doubt is, for me to print the negative binary number since MSB of newAns was 1
do I have to find 2's compliment of newAns again or not?
#include<iostream>
#include <math.h>
using namespace std;
int decimalToBinary(int n){
int ans = 0;
int i = 0;
while(n!=0){
int bit = n&1;
ans = (bit * pow(10,i)) ans;
n = n >> 1;
i ;
}
return ans;
}
int main(){
int n;
cin >> n;
if(n<0){
// if number is negative
n = n*(-1);
int ans = decimalToBinary(n);
// Find 2's compliment of the number
// 1's comp
int newAns = (~ans);
// 2's comp
newAns = newAns 1;
cout << newAns << endl;
} else {
// if number is positive
int ans = decimalToBinary(n);
cout << ans << endl;
}
}
CodePudding user response:
I think the biggest issue is a confusion about representing numbers in bases.
int values are always "binary" in memory because memory is fundamentally binary. There's no such thing as a "decimal int" vs. "binary int". Representation in a specific base is a text/string/printing concept. The int is the value itself, and a "decimal int" is how you format it for someone to read. If you want to represent that number as a "decimal" or "binary", then you should convert it to a string.
A lot of weirdness in this code. Instead of n=n*(-1);decimalToBinary(n) why not just decimalToBinary(-n)? Very confusing. But also, why are we using twos-complement at all? It's just confusing to devs (i.e. bad). Use unary operator -().
CodePudding user response:
“Binary” and “decimal” are representations of numbers as sequences of digits. For example, “123” interpreted as decimal means 1 hundred, 2 tens, and 3 ones, and “101” interpreted as binary means 1 four, 0 twos, and 1 one.
Your decimalToBinary routines does not convert to binary, because it does not produce a normal sequence of binary digits. For the number five, it produces the number one-hundred-and-one. When one-hundred-and-one is converted to decimal and printed, the output is “101”. This could be called decimal-coded-binary.
Because the value returned by decimalToBinary, which is stored in ans, is not a binary representation of n, ~ans does not take its one’s complement. When ans has the value one-hundred-and-one, decimal 101, its binary representation is 000…0001100101. The ~ operator takes the one’s complement of those bits, yielding 111…1110011010.
This is not a useful way to compute the two’s complement representation of a negative number. Depending on what you are trying to learn with this assignment, two methods of computing and displaying the two’s complement representation are:
- Once the number is read with
cin >> n, it is already in binary form, as the input routines invoked bycin >> nconvert it. Your C representation almost certainly uses two’s complement, but you can be sure by usingunsigned int u = n;, as the conversion frominttounsigned intwill effectively produce a two’s complement representation. Then you can simply write code to print the bits ofu: For each bit in it, print “0” or “1” according to what the value of the bit is. - Alternatively, if you want to work with the mathematics of binary representations, write code to convert
nto an array of characters, in which each character is “0” or “1”, according to the binary representation ofn. Ifnis negative, you can first convert its negation to binary, and then you can iterate through the array of characters to change each “0” to a “1” and vice-versa, and finally you can write code to add “1” to the binary numeral in the array. That will include writing code to carry from position to position.
