【问题标题】:what is the elegant way of dividing input bitstring into two halves将输入位串分成两半的优雅方法是什么
【发布时间】:2014-01-12 11:45:22
【问题描述】:

给定一个比特串,比如 n 位,将它分成两半的优雅方式是什么,比如 Left n/2 bits 和 Right n/2 bits 。

例如 13 是 4 位 1101,输出应该是 L = 3 (11) and R = 1 (01) 最好在 C 中使用(即使是伪代码也可以)

【问题讨论】:

  • 使用 align2( log2(n)) 然后移动 >> 加掩码
  • 嗯,你的位串结构是什么?是像乌龙,还是更长的东西?在我看来,你如何有效地将它分成两半以形成一个大型结构可能会考虑到一个字节内的起始偏移量等因素。
  • 你能显示你已经得到的代码吗?
  • 我目前需要支持至少 128 位,我还在寻找一种数据结构
  • 我认为答案很大程度上取决于您将选择的表示形式以及您是否想假设您有可用的 uint64_t 类型(代表两半)。

标签: c string bit


【解决方案1】:

请参阅以下伪代码。这适用于 32 位输入。

代码

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

#define USERSIZE 128


long long int bin2dec(long long int  ip)
{
        int count, calc;
        long long int add = 0;
        for (count = 0; count < 64; count++)
                calc = ip%10;
                add += pow(2, count) * calc;
                ip = ip/10;
                if (ip == 0) break;
        }
        return add;

}

int main()
{
        int len = -1, maskval = -1;
        long long int temp;
        long long int intLeft;
        long long int intRight;
        char * input, *left, *right;
        input = calloc (USERSIZE, sizeof *input);
        left = calloc (USERSIZE/2, sizeof *left);
        right = calloc (USERSIZE/2, sizeof *right);
        printf("Enter the number in binary\n");
        scanf("%s", input);
        len = strlen (input);
        if (len> 0)
        {
                if (len%2 == 0)
                {
                        maskval = len/2;
                        strncpy(left, input, maskval);
                        temp = atoll(left);
                        intLeft = bin2dec(temp);
                        printf("left = %lld (%lld)\n", intLeft, temp);
                        input += maskval;
                        strncpy(right, input, maskval);
                        temp = atoll(right);
                        intRight = bin2dec(temp);
                        printf("right = %lld (%lld)\n", intRight, temp);
                }
                else
                        printf("This number does not have even number of bits, pleease add a 0 before the number and enter again\n");

        }
        else
                printf("Enter a valid string\n");
        return 0;
#endif
}

输出

[sourav@infba01383 so_overflow]# ./a.out 
Enter the number in binary
100      
This number does not have even number of bits, pleease add a 0 before the number and enter again
[sourav@infba01383 so_overflow]# ./a.out 
Enter the number in binary
0100
left = 1 (1)
right = 0 (0)
[sourav@infba01383 so_overflow]# ./a.out 
Enter the number in binary
1000
left = 2 (10)
right = 0 (0)
[sourav@infba01383 so_overflow]# ./a.out 
Enter the number in binary
0000
left = 0 (0)
right = 0 (0)
[sourav@infba01383 so_overflow]# ./a.out
Enter the number in binary
1111
left = 3 (11)
right = 3 (11)
[sourav@infba01383 so_overflow]#./a.out 
Enter the number in binary
11111111111111111111111111111111
left = 65535 (1111111111111111)
right = 65535 (1111111111111111)
[sourav@infba01383 so_overflow]#

小心!!!没有检查输入的有效性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多