简体   繁体   English

在c中使用指针将十进制转换为二进制

[英]Converting decimal into binary in c using pointers

Write a function int* dec2bin(int N, int* n), which, given a natural number 0 ≤ N < 65535, computes and returns its representation in the binary numeral system.编写一个函数 int* dec2bin(int N, int* n),给定一个自然数 0 ≤ N < 65535,计算并返回它在二进制数字系统中的表示形式。 The program has to determine the coefficients ai ∈ {0,1}, i = 0,...,n − 1, such that N = (sum->n-1) ai2^i (n ≤ 16).该程序必须确定系数 ai ∈ {0,1}, i = 0,...,n − 1,使得 N = (sum->n-1) ai2^i (n ≤ 16)。

#include <stdio.h>
#include <math.h>
#include <assert.h>

int decimalToBinary(int N)
{
    int B_Number = 0;
    int c= 0;
    int ctr=0;
    while (N != 0) {
        int rem = N % 2;
        c = pow(10, ctr);
        B_Number += rem * c;
        N /= 2;
        ctr++;
    }
    return B_Number;
}
 
int main()
{
    int N;
    scanf("%d", &N);
    printf("%d", decimalToBinary(N));
    return 0;
}

I know how to make a program that converts the numbers but I don't understand why the pointer is needed for and how to implement it.我知道如何制作一个转换数字的程序,但我不明白为什么需要指针以及如何实现它。

Use an integer type capable of encoding the decimal number 1111_1111_1111_1111: use long long .使用能够对十进制数 1111_1111_1111_1111 进行编码的整数类型:使用long long

Do not use pow() , a floating point function for an integer problem.不要使用pow() ,这是一个用于整数问题的浮点函数。 It may generate value just slightly smaller than the integer expected and is slow.它可能会生成略小于预期整数的值,而且速度很慢。

long long decimalToBinary_alt(int N) {
  long long B_Number = 0;
  long long power = 1;

  while (N != 0) {
    int rem = N % 2;  // result: -1, 0, or 1
    B_Number += rem * power;
    N /= 2;
    power *= 10;  // Scale the power of 10 for the next iteration.
  }
  return B_Number;
}

Usage用法

printf("%lld\n", decimalToBinary(N));

Another way...其它的办法...
This was written to print the binary representation of a value (left-to-right).这是为了打印一个值的二进制表示(从左到右)。 Instead of printing, you could simply assign the 0/1 (left-to-right) to a passed array (of 16 integers), then return the number of assigned integers to the calling function to print them from a loop.您可以简单地将 0/1(从左到右)分配给传递的数组(16 个整数),而不是打印,然后将分配的整数数返回给调用函数以从循环中打印它们。

int main() {
    for( int i = 253; i <= 258; i++ ) {
        printf( "Decimal %d: ", i );
        unsigned int bitmask = 0;
        bitmask = ~bitmask;
        bitmask &= ~(bitmask >> 1); // High bitmask ready

        // skip over leading 0's (optional)
        while( bitmask && (bitmask & i) == 0 ) bitmask >>= 1;

        // loop using bitmask to output 1/0, then shift mask
        do {
            putchar( (bitmask & i) ? '1' : '0' );
        } while( (bitmask >>= 1) != 0 );

        putchar( '\n' );
    }
    return 0;
}

Your function does not have the required parameters and return value.您的函数没有所需的参数和返回值。

int* dec2bin(int N, int* n)
{
    unsigned uN = N;
    
    for(int bit = 15; bit >= 0; bit--)
    {
        *(n + 15 - bit) = !!(uN & (1U << bit));
    }
    return n;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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