Форум » C/C++ для начинающих (C/C++ for beginners) » int to bit » Ответить

int to bit

ascmax: The task is follow: Implement function char* int2bin(int x) which would return a string representation of int x in binary form. I.e.: 00001001 10001110 11011011 10011011 How Can I make my code output that ? So far I have decimal number returned as binary but not as the display way of what the task wants. #include <stdio.h> #include <math.h> int n=5; void getbits(unsigned int x) { for(int i=(sizeof(int)*n)-1; i>=0; i--) (x&(1<<i))?putchar('1'):putchar('0'); printf("\n"); } int main() { int j = 3567; printf("Decimal: %d Binary: ", j); getbits(j); return 0; } Thank you

Ответов - 1

Сыроежка: I do not understand what this magic definition [pre2] int n=5; [/pre2] means in your code. As for the question then you need to allocate dynamically a character array and write character representations of bits into the array instead of the standard output stream as you do in your code.



полная версия страницы