468,457 Members | 1,593 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

Binary Data Types in C

Hi
Is there a data type in C which accepts numbers as binary and any way to add binary numbers directly??
I am using Windows OS.
Mar 10 '08 #1
8 16304
Banfa
9,051 Expert Mod 8TB
If you mean is there a way to write a binary constant then in Standard C no (although I have seen a few compilers with extensions for binary constants).

Nowadays most people would use hexadecimal because binary produces too many digits where as hexadecimal doesn't and each hexadecimal digit is exactly 4 binary digits making the conversion from binary to hex easy (or at least much much easier than decimal to binary). as you only have to remember 16 bit patterns to be able to convert any number.
Mar 10 '08 #2
thanks
i wrote a code for decimal to binary conversion(including mantissa) but in the final binary output its showing only few digits and representing the remaining numbers in exponential form(i cannot afford to lose those numbers)-to be precise i want 23 digits of mantissa and 8 digits of integer part.
i decleared the final output as long double.(Windows OS).
Any way to solve this??
Mar 10 '08 #3
Laharl
849 Expert 512MB
Well...the compiler is going to use the IEEE floating-point standard, which is exponential in nature. It uses 8 bits for the exponent and (effectively) 24 for the mantissa. Long doubles would use a higher number for each part, but it's still exponential in nature, as that gives a far, far higher range of representable numbers.
Mar 10 '08 #4
Sick0Fant
121 100+
Well, first of all, I believe you *can* declare variables bit-wise. I believe you do it by declaring the variable as "int(8) myVar;" (if you want 8 bits). You can add them bit-wise with the "|" operator and subtract them using the two's complement.

As for displaying them, I had to do a similar thing in school, and I ended up using a character array to display the characteristic and mantissa.
Mar 10 '08 #5
i dont want to print the number with mantissa...but i want to *return* with mantissa...so i used the following code..
the problem is that in 'f' 20th to 23rd bits of manitissa are getting rounded off
For Example :
if number to be converted is 0.56
the output is f =1.00011110101110000107812e-01


Expand|Select|Wrap|Line Numbers
  1. d= 1;
  2.     for(i=0;i<23;i++)
  3.         {
  4.         temp=1/(pow(10,d));
  5.         f= f+(c[i]*temp);
  6.         printf("%d",c[i]);        
  7.         d++;
  8.  
  9.         }
  10.  
  11.     printf("\n %.23Le \n ",f);
  12.  
{ c[i] contains the mantissa value with 23 digits}
i hav to return a binary number...
Mar 10 '08 #6
Sick0Fant
121 100+
Well, if you declare a variable bitwise, you can just return the variable.

Edit:

You can't declare variables to be a certain length as it turns out, but you can read any bit of a variable. Could you tell me why, if you're not to print the number, it is important to have the binary representation of the number?

Second edit:

I think I see what you want. You want to feed the decimal floating point into the function and print the binary(?) If so, you've run into a limitation of printf. I suggest manually printing each bit of the number individually (via loop, using the bit-shift operator "<<"). That way, printf won't try to round for you.
Mar 10 '08 #7
But in shifting, it converts the number into decimal and shifts(becoz we are declaring it as a number to the base 10.
So shifting the number will not give you the extra digits i want to know if its correct (taking that its the problem with printf )
any way thanks a lot
Mar 11 '08 #8
Banfa
9,051 Expert Mod 8TB
If you want to print the string of bits representing a given floating point number then you do not want to be using floating point arithmetic which will inherently introduce rounding errors.

You need to get the location of the floating point number in memory and print the bits directly (or copy them to a string).
Mar 11 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Sebastian Kemi | last post: by
103 posts views Thread by Steven T. Hatton | last post: by
4 posts views Thread by knapak | last post: by
26 posts views Thread by Patient Guy | last post: by
9 posts views Thread by gamehack | last post: by
1 post views Thread by Michael | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.