473,503 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Conversion of decimal to binary in C programming n store in arrays

8 New Member
Can someone give me an idea to convert decimal numbers into its binary equivalent and store each binary in arrays in C programming language??
Oct 1 '08 #1
6 9407
boxfish
469 Recognized Expert Contributor
In C, all the number types are, somewhere deep in your computer, actually stored as a row of binary bits, so I wouldn't call a number "decimal" unless it's decimal digits were stored in a string. But aside from that, to get a bit at a specific position in an int, you can make a int that has only a bit in the place in question, compare it to the original int with the bitwise and operator (so that the result has all false bits unless both the ints have a bit in the same position), and then cast the result to a bool. Hope this helps.
Oct 1 '08 #2
r035198x
13,262 MVP
What ideas do YOU have for it yet?
Oct 1 '08 #3
Zenoba
8 New Member
What ideas do YOU have for it yet?
i have idea for only one decimal number
that is input a number then the program gives you its binary equivalent
the code is as follows:

Expand|Select|Wrap|Line Numbers
  1. int num,no_bits;
  2.  
  3. printf("Enter number");
  4. scanf("%d",&num);
  5. no_bits=count_bits(num);
  6. printf("number of bits %d",no_bits);
  7. bin_print(num,no_bits);
  8.  
  9. count_bits(int n) //function to count number of bits 
  10. {
  11. int i=0;
  12. for (;n!=0;n=n>>1)
  13. i++;
  14. return(i);
  15. }
  16. bin_print(int x, int n_bits)
  17. /* function to print decimal numbers in binary format */
  18. {
  19. int j;
  20. printf("no. %d in binary \t",x);
  21. for(j=n_bits-1; j>=0;j--)
  22. printf("%i",(x>>j) & 01);
  23. }
but that's only for one number
i have to do it for a series of numbers stored in an array and store the binary ones in another array
Oct 1 '08 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
There is a simple algorithm for this.

Given the number 22 which has these bits:

16 8 .. 4 .. 2 .. 1
1.. 0 .. 1 .. 1 .. 0

To see if the bit in the 4 position is set, divide the number by 4 and take the result %2. That is:

22/4 = 5 and 5%2 is 1 --> the 4 bit is set
22/8 = 2 and 2%2 is 0 --> the 8 bit is not set

No need for fancy bit shifting.
Oct 1 '08 #5
whodgson
542 Contributor
My book C++ In Plain English by Brian Overland has an interesting function for this:
Expand|Select|Wrap|Line Numbers
  1. void print_binary(short input_field)
  2. {
  3.     int i,bit_set;
  4.  
  5.     while (i<=16)
  6.     { bit_set = ((Ox8000 & input_field) >0);
  7.       printf("%d",bit_set);
  8.       input_field = input_field  << 1;
  9.       i++;}
  10. }
I presume the equivalent can be written in C
Oct 17 '08 #6
whodgson
542 Contributor
This is a function which emulates weaknessforcats' algorithm
Expand|Select|Wrap|Line Numbers
  1. int convert(int n)
  2. {int k=1;
  3.  while(k<n)//find the most significant bit
  4.   k*=2;
  5.  if(k>n)//fix the overshoot
  6.   k=k/2;
  7.  
  8.  while(k>0)
  9.  {
  10.  if(int(n/k)%2==0)cout<<"0 ";
  11.  else cout<<"1 ";
  12.  k=k/2;
  13.  }
  14. }
Oct 18 '08 #7

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

Similar topics

17
6102
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
2
2793
by: Brian van den Broek | last post by:
Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a...
2
16022
by: Raja | last post by:
IS there any inbuilt decimal to binary conversion function in C++
687
22803
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
5
27516
by: Richard Thrasher | last post by:
Writing my very first C# program, I found what appears to be a compiler error. The statement int temp = (int)(100 * 36.41); assigns the value 3640 to temp. I've tried this code with various...
3
17821
by: Amjad | last post by:
Hi, Are there any built-in methods in VB.NET that convert one number in one format to its equivalent in another format? For example I want to convert the number 162 (decimal) to 10100010...
28
5810
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
4
4369
by: dondigitech | last post by:
I want to convert hex to binary without losing bits. I want to preserve the 8-bits because I ultimately need a 24-bit string to grab information from. I am just using this line of code for the...
6
3370
by: i_robot73 | last post by:
I have a file, containing hex values for dates (MMDDYYYY)<status code><??such as: ...
0
7199
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
7076
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
7323
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...
1
6984
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
5576
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4670
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.