1

10進数と2進数の基数システムを変換する関数を書いていましたが、元のコードは次のとおりです。

void binary(int number)
{
    vector<int> binary;

    while (number == true)
    {
        binary.insert(binary.begin(), (number % 2) ? 1 : 0);
        number /= 2;
    }

    for (int access = 0; access < binary.size(); access++)
        cout << binary[access];
}

しかし、私がこれを行うまでは機能しませんでした:

while(number)

どうしたの

while(number == true)

そして、2つの形式の違いは何ですか?前もって感謝します。

4

2 に答える 2

8

と言うとwhile (number)numberであるint、はタイプに変換されboolます。ゼロの場合はになりfalse、ゼロ以外の場合はになりtrueます。

と言うとwhile (number == true)、は(になる)trueに変換され、と言ったのと同じです。int1while (number == 1)

于 2011-04-24T07:57:31.157 に答える
0

これが私のコードです....

    #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<unistd.h>
#include<assert.h>
#include<stdbool.h>
#define max 10000
#define RLC(num,pos) ((num << pos)|(num >> (32 - pos)))
#define RRC(num,pos) ((num >> pos)|(num << (32 - pos)))

void tobinstr(int value, int bitsCount, char* output)
{
    int i;
    output[bitsCount] = '\0';
    for (i = bitsCount - 1; i >= 0; --i, value >>= 1)
      {
             output[i] = (value & 1) + '0';
      }
}


  int main()
   {
    char s[50];
    tobinstr(65536,32, s);
    printf("%s\n", s);
    return 0;
   }
于 2012-02-03T09:49:01.420 に答える