4

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

2
  • Let's see your java version. The C version should be quite similar. Commented Feb 3, 2010 at 9:11
  • @Andreas: this is meta. I'll delete my comments here in due course, and I've started a Meta thread for this discussion: meta.stackexchange.com/questions/38077/… Commented Feb 3, 2010 at 10:17

6 Answers 6

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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
@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.
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
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.
3

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

1 Comment

Liked the pun in "bit by bit" :-). +1
0

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

2 Comments

yes actually that makes sence, but i think i see a problem, the values will be in reverse wont they?
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
0

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.

Comments

0

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.

Comments

0

One solution to find an integer to binary as follows : using bitshifting

int DintoB(int num){
int a=31;  enter code here`//as 32 bit number int.

while (a>=0){
    int n=num>>a;//bit shifting
    if(!(n%2))printf("0");
    else printf("1");
    a--;
}
printf("\n");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.