472,779 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 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 17545
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.