1. ## Coding Binary

Good Morning,
I am working on a project for school that requires me to compute binary numbers and I am having trouble with it. It needs to be able to handle an input of binary, octal, decimal, and hexadecimal. For each input it needs to have the output of the sign/magnitude, one's complement, and two's complement.

How do I do something like this?

Thank you very much,
Mike

Calculate output
Show output to user

3. How do I calculate the output in C?

Thanks,
Mike

For this, I assume that you were given a sample input file/text of some type? Depending on how the data is to be presented, there thousands of ways to read data.

Provide some example input and post what code you have so far.

5. input (in terminal): ./mynumber hex -2D
output:
Sign/Mag: 1000000000101101
1's: 1111111111010010
2's: 1111111111010011

6. okay, so what you'll want to do is to look at index one of argv (I would toupper or tolower that joker for comparison purposes), do a strncmp against a hard-coded array of predefined keywords (looking for which one it is), then put that value in a switch to handle the processing of index two.

Now, POST SOME CODE!!!

7. Here is what I have so far... I check it to make sure all of the arguments are there, then convert it to binary, then display. The problem I am having is converting everything to binary and then displaying it in either sign/mag, 1's, or 2's.

Mike

Code:
```#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[]){
// Check to make sure all of the arguments are inputted, otherwise print invalidUsage()
if (argc != 3){
// Display invalid usage
invalidUsage();
} else {
// Convert the input into binary
long v = convertInput(argv[1], argv[2]);
// Print the output of the conversions
doOutput(v);
}
return 0;
}

int convertInput(char *format, char *value){
// Choose the type of input
if(strcmp(format, "bin") == 0){
// If the input is binary
convertBinary(value);
} else if(strcmp(format, "oct") == 0){
// If the input is octal
convertOctal(value);
} else if(strcmp(format, "dec") == 0){
// If the input is decimal
convertDecimal(value);
} else if(strcmp(format, "hex") == 0){
// If the input is hexadecimal
convertHex(value);
} else {
// If it is none of the above
invalidUsage();
}
}

int invalidUsage(){
// If the input is incorrect, display mynumber usage
printf("Usage: ./mynumber [bin binary number]\n\t\t  [oct octal number]\n\t\t  [dec decimal number]\n\t\t  [hex -hexadecimal number]\n");
}

doOutput(long value){
// Sign/Magnatude output
doOutputSignMag(value);
// One's Complement output
doOutputOnes(value);
// Two's complement output
doOutputTwos(value);
}

int convertBinary(char *str){

}

int convertOctal(char *str){

}

int convertDecimal(char *str){

}

int convertHex(char *str){

}

doOutputSignMag(long value){

}

doOutputOnes(long value){

}

doOutputTwos(long value){

}```

8. Okay, so what happens if i put "HeX" as the identifier?

PUTTING YOU IN THINKING MODE:
What are you attempting to accomplish? (Why?)

How would you do this if you didn't have a computer? (That'd be, work out the solution on paper or in your head).

You'll find that you don't need a function for each type, you'll just need to be smart about the modification of a couple of functions.

9. Everything is in binary on a computer. What you want to do is this:
-Calculate one's and two's compliment
-Display in binary format

How to calculate one's and two's compliment? That is rather simple if you know how to do it with a pen and paper. Do you know how to do bitwise operations in C?

How to display in binary format? It would be nice if printf() had this option. But it doesn't. If you google search for this you will find some answers. Generally, check if digit is 0 or 1. If 0 prinft("0"). If 1 printf("1"). Again, you will probably want to use bitwise operators.

Don't get confused with the format of data. Everything is binary when it comes to calculations. In C you can use XOR, AND, OR operators on data. So you should have no problem.

If you are still stuck provide at least some pseudo-code on how to calculate one's or two's complement

10. Originally Posted by C_ntua
Everything is binary when it comes to calculations. In C you can use XOR, AND, OR operators on data. So you should have no problem.
Not exactly true, when it comes to floating point numbers.

Quzah.

11. I read the cprogramming.com bitwise tutorial and it helped me a bit... I hope.
Below is updated code for what I think I have figured out.
I have a feeling that in using int I am getting some kind of overflow... which to me also means I am not doing this correctly.
I am also not sure what to do with negative numbers if they were to appear.

Thank you very much,
Mike

Code:
```int convertBinary(char *str){
return atoi(str);
}

doOutputSignMag(int value){
int i;
for(i = 15; i >= 0; i--){
if(value & 1 << i){
printf("1");
} else {
printf("0");
}
}
printf("\n");
}

doOutputOnes(int value){
int i;
for(i = 15; i >= 0; i--){
if(~value & 1 << i){
printf("1");
} else {
printf("0");
}
}
printf("\n");
}

doOutputTwos(int value){
int i;
value = value + 1;
for(i = 15; i >= 0; i--){
if(value & 1 << i){
printf("1");
} else {
printf("0");
}
}
printf("\n");
}```

12. If you are using signed variables, then yes you'll get some overflow when you hit the sign bit. It also depends on if you have 16bit or 32bit integers (or 64).

Quzah.

13. How can I tell if it is signed bits and if it is 16 or 32bit integers?

Thanks,
Mike

14. Use the sizeof operator.
Code:
`printf( "The size of my integers are %d * %d.\n", sizeof( int ), CHAR_BIT );`
If you use CHAR_BIT, it's in <limits.h>.

Quzah.

15. The result from that is 4*8, which is 32.
The problem I am working on wants 16 bits.

So, am I on the right track with doing the sign/magnitude, ones complement, and twos complement for doing the output?

Thanks,
Mike