本文介绍了转换龙64位十进制为二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,我想要做到的是相当简单的,虽然我仍然有问题。

OK, what I'm trying to accomplish is fairly straightforward although I'm still having issues.

这是我的code:

- (NSString*)toBin:(long)dec
{
    long num = dec;
    NSString *res = [NSString string];

    for (long i=63; i>=0; i--)
    {
        long div = 1<<i;
        if ((num&div)==div) res = [res stringByAppendingString:@"1"];
        else res = [res stringByAppendingString:@"0"];
    }

    return res;
}

这就是我正在测试它:

    for (long i=1; i<10; i++)
    {
        NSLog(@"%u = %@",i,[self toBin:(long)i]);
    }

但是,在上述的输出是:

However, the output of the above is :

1 = 0000000000000000000000000000000100000000000000000000000000000001
2 = 0000000000000000000000000000001000000000000000000000000000000010
3 = 0000000000000000000000000000001100000000000000000000000000000011
4 = 0000000000000000000000000000010000000000000000000000000000000100
5 = 0000000000000000000000000000010100000000000000000000000000000101
6 = 0000000000000000000000000000011000000000000000000000000000000110
7 = 0000000000000000000000000000011100000000000000000000000000000111
8 = 0000000000000000000000000000100000000000000000000000000000001000
9 = 0000000000000000000000000000100100000000000000000000000000001001

因此​​,它的几乎的正确(如最后32位),但它似乎被复制自身为顶级32位。我猜有一些东西跟我的尺寸,但的sizeof(长)收益 8 。任何想法?

So, it's almost correct (as for the last 32 bits), though it seems to be duplicating itself for the top 32 bits. I guessed it had something to do with my long size, but sizeof(long) returns 8. Any ideas?

推荐答案

这前pression:

This expression:

long div= 1<<i;

是一个int,而不是一个long.So只有32位的整数(原谅,如果我只说我的机器)。所以只产生一个64位的前pression:

Is an int, not a long.So you get an integer of just 32 bits (pardon if I speak only for my machine).So just produce a 64 bit expression:

long div = 1l<<i;

这篇关于转换龙64位十进制为二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-10 20:59
查看更多