C Programming - Converting an Integer to Binary

I was hoping after some advice against decisions, as this is homework, and I want to solve it myself.

I am primarily very new to C. In fact, I have never done this before, although I have previous java experience from modules at the university.

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

Can anyone suggest some ideas on how I will deal with this. Obviously, I don’t want the code or anything else, just some ideas as to what opportunities to explore as current, I'm a little confused and don't have an attack plan. Well, make it confuse: D

Many thanks

+4
source share
6 answers

Think about what you can do with bitwise operators.

eg. you can check if bit 1 or 0 is equal as follows:

int bit = value & 1; 

You can also shift bits into int as follows:

 val = val >> 1; 

To check the ith bit in int, you can do this:

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

Hope this is enough to get you started.

+6
source

Break it in half. Use shift. Processing more than one bit at a time can be performed using a lookup table.

+3
source

You can save the output in an array of characters

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

or you can "save" the output on the screen

 printf("1"); 

although this can be argued as using library functions

0
source

You might want to remember how to manually convert numbers to binary representations. This may not be the most efficient way to do this, but it feels more natural than just playing with switch operators.

To convert a number to a binary representation, you basically look at which parts of 2 ^ I fit into that number and build on that binary number.

0
source

I suppose you need to do your homework, so here is my solution. :)

I was just trying to write an ascii-to-binary + binary-to-ascii to C converter, by coincidence. The binary for ascii was pretty simple with the strtol () function and all, and my code for converting a character (which is practically int) was as follows:

 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 the symbol (change the parameter and type of size according to your needs) in the correct order (which, in my opinion, is the biggest value), unlike many other solutions.

I especially like this solution because it is very short. Then it does not save the value as a string or anything like that.

Edit: I think you can also substitute fputs() call with

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

Maybe microseconds are faster? Dunno.

0
source

One solution for finding an integer in binary as follows: using bitrate

 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"); } 
0
source

Source: https://habr.com/ru/post/1300152/


All Articles