Curtisq 0 Newbie Poster

So my problem is writing a program that asks the user to input a number wich is stored with scanf then print the binary representation of that number in 32 bits, as in if the number is 1 i would need 31 0's and a 1.

#include <stdio.h>

void dec_bin(long long int number);

int main(void) {
 long long int input = 0;

 printf("Enter a number (0-8589934591): ");
 scanf("%lld", &input);

 (input >= 0) && (input < 8589934592) ? dec_bin(input) : exit(1);

 return 0;
}

void dec_bin(long long int number) {
 long long int x, y;
 x = y = 0;

 for(y = 32; y >= 0; y--) {
  x = number / (1 << y);
  number = number - x * (1 << y);
  printf("%lld", x);
 }
 printf("\n");
}

This prints out whatever number is entered followed by a bunch of zeros, so if i enter 85 it will print 8500000... When i change (input >= 0) && (input < 8589934592) to input < 512 y = 9 or some other smaller number the program works fine but when i try to make the jump to 32 bits it stops working the way it should. the long long ints were my attempt to fix it and it didnt work. If anyone could tell me where its going wrong that would be great.


EDIT - fixed just set y = 31 it makes 32 bits as its to >= 0, anything 32 or over breaks im not sure why but it doesnt apply to my problem. Also changed the number you can enter up to to 2^31 - 1 rather than the number it is now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.