tags:

views:

538

answers:

5

Hi guys - i was hopefully after some tips opposed to solutions as this is homework and i want to solve it myself

I am firstly very new to C. In fact i have never done any before, though i have previous java experience from modules at university.

I am trying to write a programme that converts a single integer in to binary. I am only allowed to use bitwise operations and no library functions

Can anyone possibly suggest some ideas about how i would go about doing this. Obviously i dont want code or anything, just some ideas as to what avenues to explore as currenty i am a little confused and have no plan of attack. Well, make that a lot confused :D

thanks very much

+3  A: 

Tear it apart bit by bit. Use shifting. Handling more than one bit at a time can be done with a lookup table.

Ignacio Vazquez-Abrams
Liked the pun in "bit by bit" :-). +1
Alok
+5  A: 

Think about about what you can do with bitwise operators.

E.g. you can test whether a bit is 1 or 0 like this:

int bit = value & 1;

You can also shift the bits in an int like this:

val = val >> 1;

To test the i'th bit in an int you can do this:

int bit = (value >> i) & 1;

Hopefully that's enough hints to get you started.

Paul R
so would it make sence to loop over the bits, recording the position at which there was a one, if there isnt then add a zero?if this is possible then how do i loop over bits in an integer, and where would i save the results - im quite sure i am not allowed to use an array
leo
@rob you need to learn how the shift operators << and >> work, but essentially you would want to test and shift down one position, test next one and shift one more. there are many good tutorials on bitwise operations in C.
kb
ok that makes perfect sence and is exactly what i have written down in psudocode on my paper, however - when i test these numbers how do i store the results. for example position 1 may have a 1, now where is it stored, position 2 maybe a 0, third maybe 1, fourth maybe 1 fith a 0 and so on
leo
You should also read about **Big Endian** and **Little Endian**. If you print the Most Significant Bit (MSB) first, it will be Big Endian format. I would ask the instructor which format the results should be.
Thomas Matthews
A: 

You could store the output in a character array

char output[33]; // for 32 bit integers, plus a null terminator if you need one
output[n] = '1';

or you could 'store' the output on the screen

printf("1");

though this could be argued as using a library functions

David Sykes
yes actually that makes sence, but i think i see a problem, the values will be in reverse wont they?
leo
You can store and read the values in the array in different directions. For printf you have to get the bits in the right order
David Sykes
A: 

You might want to remember how you convert numbers to binary representations by hand. It might not be the most efficient way to do this, but it feels more natural than just playing with the shifting operators.

To convert a number into binary representation, you basically look which 2^i parts fit in that number and construct your binary number based on that.

poke
A: 

I figure you should be done with your homework by now so here's a solution of mine. :)

I was just now trying to write an ascii-to-binary+binary-to-ascii converter in C, coincidentally. Binary to ascii was fairly simple with the strtol() function and all, and my code for converting a character (which is practically an int), was this:

void chartobinf(int c, FILE* out)
{
  size_t n = 8*sizeof(unsigned char);
  while (n-- > 0)
    fputs(((c >> n) & 1) ? "1" : "0", out);
}

This prints out a character (change parameter and sizeof types to suit your needs), in the correct order (which in my opinion is largest value first), as opposed to a lot of other solutions.

I especially like this solution because it is very short. Then again it doesn't save the value as a string or anything like that.

Edit: I guess one could also substitude the fputs() call with a

fputc(((c >> n) & 1) ? '1' : '0', out);

Might be microseconds faster? Dunno.

Victor