hello everyone
i need code which converts any number between -128 to 127 into binary
this is not my homework but a small part of a task assigned to me which states i can use the internet for help
any help appreciated
m600
Printable View
hello everyone
i need code which converts any number between -128 to 127 into binary
this is not my homework but a small part of a task assigned to me which states i can use the internet for help
any help appreciated
m600
What exactly do you mean by that? For example, are you trying to print out, or store as a string, a binary representation of the number? Also, what signed binary representation do you have in mind, e.g., two's complement?Quote:
i need code which converts any number between -128 to 127 into binary
That is, it is homework, whether it is graded or not graded, whether you are being paid for it or it is just a challenge by a friend made over dinner.Quote:
Originally Posted by m600
ok i need it to convert the number into a binary representation in 8 bits, so 5 would be 00000101
thanks
What would -1 be?
2's compliment so you add 256 to any minus number, then convert it
Good. I suggest that you take a look at std::bitset.
ok well this is the code i have so far but it wont let me use scanf which is commented out atm
Code:#include <stdio.h>
#include <DBOS\GRAPHICS.H>
#include <STDLIB.H>
int main()
{
int input=100;
int bin_no[8]={0,0,0,0,0,0,0,0},i=7;
printf("enter number to be converted\n");
//scanf("%d",&input);
if(-128<input && input<128)
{
if(input<0)
{
input= input + 256;
}
do
{
bin_no[i] = input % 2;
input = input / 2;
i--;
}while(input!=0);
for(i=0 ; i<8 ; i++)
{
printf("%d", bin_no[i]);
}
printf("\n");
}
system("pause");
return 0;
}
Is this supposed to be C or C++?
c++ .............................
Do you have any special reason for using C-style I/O instead of C++-style I/O then? Furthermore, you do not need <DBOS\GRAPHICS.H> here, so leave it out.Quote:
Originally Posted by m600
If you had taken my suggestion of std::bitset, you could come up with a solution along these lines:
Of course, you should apply your input error checking as you did in your current code.Code:#include <iostream>
#include <bitset>
int main()
{
using namespace std;
cout << "Enter an integer in the range [-128, 127] to be converted: ";
int input;
cin >> input;
cout << input << " in 8-bit two's complement binary: "
<< bitset<8>((input < 0) ? (input + 256) : input) << endl;
}
Try this - it works - C source
Code:#include <stdio.h>
void dec2bin(long decimal, char *binary);
int main()
{
long decimal;
char binary[80];
printf("\n\n Enter an integer value : ");
scanf("%ld",&decimal);
dec2bin(decimal,binary);
printf("\n The binary value of %ld is %s \n",decimal,binary);
getchar(); // trap enter
getchar(); // wait
return 0;
}
//
// accepts a decimal integer and returns a binary coded string
//
void dec2bin(long decimal, char *binary)
{
int k = 0, n = 0;
int neg_flag = 0;
int remain;
int old_decimal; // for test
char temp[80];
// take care of negative input
if (decimal < 0)
{
decimal = -decimal;
neg_flag = 1;
}
do
{
old_decimal = decimal; // for test
remain = decimal % 2;
// whittle down the decimal number
decimal = decimal / 2;
// this is a test to show the action
printf("%d/2 = %d remainder = %d\n", old_decimal, decimal, remain);
// converts digit 0 or 1 to character '0' or '1'
temp[k++] = remain + '0';
} while (decimal > 0);
if (neg_flag)
temp[k++] = '-'; // add - sign
else
temp[k++] = ' '; // space
// reverse the spelling
while (k >= 0)
binary[n++] = temp[--k];
binary[n-1] = 0; // end with NULL
}
Here is my full c++ code-like tutorial for you, connecting everything and even adding it all up for ya.
Code:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int N = 123; // 32-bit integer.
for (int j = 0; j < 32; ++j)
{
// Shift the bits of N over to the right one at a time.
// This loop takes 'j' from 0 to 31. N >> j
cout << ((N >> j) & 0x1);
// The test looks to see if the first bit of (N>>j) is 'on' or 'off'.
// The test returns true for on; 1, False for off; 0.
}
// Output:
// 11011110000000000000000000000000
// (32 bits)
// 123 = 1101 1110 0000 0000 0000 0000 0000 0000
// 1 1 = 1
// 2 1 = 2
// 4 0 = 0
// 8 1 = 8
// 16 1 = 16
// 32 1 = 32
// 64 1 = 64
// 128 0 = 0
// ... = 0
// 1 + 2 + 8 + 16 + 32 + 64 = 123
// Success! :)
return 1;
}
Spoon-feeding central!
m600 has already posted code in post #7 that looks like more or less a correct implementation, which is why I have no qualms about posting a proper C++ example. I don't know why John Layton bothered posting a C example along the same lines, though syneii's example at least demonstrates a slightly different idea which requires some adaptation.Quote:
Originally Posted by rags_to_riches
Gee, I did in fact miss that he OP posted code. Oops, sorry about that!