#include<iostream>
#include<string>
#include<limits>
#include<iomanip>
std::string char2bin( char c )
{
std::string result( 8, '0');
int bit = 8;
while( bit-- )
{
result[bit] += (c & 0x01);
c >>= 1;
}
return( result );
}
template<typename T>
std::string tobin( T t )
{
std::string result;
const size_t size = sizeof( T );
char* p = (char*) &t + size - 1;
unsigned int s = size;
while( s-- )
result += char2bin( *p-- );
return( result );
}
int main()
{
double d = 1.2345;
float f = 1.2345;
std::cout<<std::setprecision(std::numeric_limits<float>::digits10+1)<<d<<" (float) in binary is "<<tobin(f).c_str()<<std::endl;
std::cout<<std::setprecision(std::numeric_limits<double>::digits10+1)<<d<<" (double) in binary is "<<tobin(d).c_str()<<std::endl;
}
easy, 1011. in binary of course. convert 1011 binary to decimal you get 11.
If you want to add numbers in different bases, in this case decimal and binary, or do any other calculation that involves different bases for that matter, you have to convert all numbers to a single system first - for example, all to decimal. Then you can do the operation. It is really up to you in what base you represent the final answer. In this example, you can convert back to binary, for example.
2 decimal, or 10 binary.
#include<stdio.h> #include<stdlib.h> main() { int number,binary[10000],b=0; printf("Enter decimal number "); scanf("%d",&number); printf("\nBinary: "); for(;number;number/=2,b++) binary[b]=number%2; for(b--;b>-1;b--) printf("%d ",binary[b]); }
111011000 (decimal 472). The sum is 257+215.
0111 = decimal 7 0011 = decimal 3 ____ 1010 = decimal 10 ____
In binary it's the equivalent of decimal 3.
201 in decimal1001 in binary9 in binary translated to decimal
1000110. In decimal, the sum is 23+47=70.
To add these two binary numbers, we can first convert them to decimal. 111111 in base 2 is equal to 63 in base 10, and 10001 in base 2 is equal to 17 in base 10. Adding these two decimal numbers gives us 63 + 17 = 80 in base 10. Finally, we convert 80 back to binary to get the final answer, which is 1010000 in base 2.
The first number (01001101) is equal to the decimal number 77. The second number (00100010) is equal to the decimal number 34. If you add the two together in decimal, you get 111. Expressed as a binary number, 111 is equal to 01101111.
In a binary system, where only the digits 0 and 1 are used, the addition of 2 (represented as 10 in binary) and 2 (also represented as 10 in binary) equals 100 in binary, which is 4 in the decimal system. Therefore, in this specific context, 2 plus 2 equals 11.