简体   繁体   中英

How to convert decimal to binary in 64 bits?

so I have this code

int main()
{
  int n, c, k;

  printf("Enter an integer\n");
  scanf("%d", &n);

  printf("%d in binary is:\n", n);

  for (c = 31; c >= 0; c--)
  {
    k = n >> c;

    if (k & 1)
      printf("1");
    else
      printf("0");
  }

  printf("\n");

  return 0;

}

It converts decimal into binary but only in 32 bits. When I change it into 64 bits it doesn't work (it seems like it just doubles the result from 32 bits). At the same time it works fine with 8 or 4 bits etc. What am I doing wrong?

It converts decimal into binary but only in 32 bits. When I change it into 64 bits it doesn't work (it seems like it just doubles the result from 32 bits).

The problem is here.

  int n, c, k;

  printf("Enter an integer\n");
  scanf("%d", &n);

n is an int which can be as small as 16 bits. It could be 64 bits, but it's probably 32 bits. When you try to enter a 64 bit number you'll get garbage.

#include <stdio.h>

int main() {
    int n;

    printf("sizeof(int) == %zu\n", sizeof(int));

    printf("Enter an integer\n");
    scanf("%d", &n);

    printf("n = %d\n", n);
}

$ ./test
sizeof(int) == 4
Enter an integer
12345678901
n = -539222987

Instead, you can use a long long int which has a minimum size of 64 bits or int64_t from stdint.h which is exactly 64 bits. I have a preference for using the explicit width types in code that requires a specific width to make it more obvious to the reader.

long long int uses %lld for scanf and printf while int64_t uses macros from inttypes.h . The code below takes advantage of C automatically concatenating constant strings; "foo" "bar" and "foobar" are equivalent.

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

int main() {
    int64_t n;

    printf("Enter an integer\n");

    scanf("%"SCNd64, &n);

    printf("n = %"PRId64"\n", n);
}


$ ./test
Enter an integer
12345678901
n = 12345678901

The problem is your n & k variables' data type. They are integer. Check your platform's data type size using sizeof(int).

When you change to 64-bits, these variables can't hold 64-bit values.

HTH!

the following code is one way to handle the problem:

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
    long long unsigned int n;
    long long          int c;
    long long unsigned int k;

    printf("Enter an integer\n");
    if( 1 != scanf("%llu", &n) )
    {
        perror( "scanf for 64 bit number failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, scanf successful

    printf("%llu in binary is:\n", n);

    for (c = 63; c >= 0; c--)
    {
        k = n >> c;

        if (k & 1)
            putc('1', stdout);
        else
            putc('0', stdout);
    }

    printf("\n");

    return 0;
} // end function: main

and here is the output from a typical run of the program:

Enter an integer
6789097860397846  
6789097860397846 in binary is:
0000000000011000000111101010011000000110010100000111101100010110

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM