473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 17615
Banfa
9,065 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,065 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

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

Similar topics

6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
8
by: Jerry | last post by:
I have an off-the-shelf app that uses an Access database as its backend. One of the tables contains a field with an "OLE Object" datatype. I'm writing some reports against this database, and I...
4
by: knapak | last post by:
Hello I'm a self instructed amateur attempting to read a huge file from disk... so bear with me please... I just learned that reading a file in binary is faster than text. So I wrote the...
26
by: Patient Guy | last post by:
Has anyone written code that successfully manipulates binary file data using Javascript? It might---and in the case of doing I/O, will---make use of browser- specific functions (ActiveX/COM with...
9
by: gamehack | last post by:
Hi all, I've been wondering when I write a structure like: struct { int a; unsigned int b; float c; } mystruct;
14
by: Vertilka | last post by:
I need to read binary data file written by C++ program, using my C# application. How do i marshal the bytes i read with my C# code to .NET types. The data is numbers (integers float doubles...
1
TMS
by: TMS | last post by:
I'm trying to write an address book that is based on a binary tree. I'm devloping in Visual C++ (I blew up my Ubuntu with the new dist, so no EMACS), starting with the basics: #ifndef...
1
by: Michael | last post by:
I have a solution for this, but it feels wrong. If anyone could offer a better one, I'm all ears. (Or technically, eyes.) Basically, I have a bunch of classes. For concreteness, one is a...
11
by: itdevries | last post by:
Hi, I'm trying to convert some char data I read from a binary file (using ifstream) to a float type. I've managed to convert the int types but now I need to do the float types as well but it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.