c - 将输入位串分成两半的优雅方法是什么

标签 c string bit

给定一个 n 位的位串,将它分成两半的优雅方法是什么,比如左 n/2 位和右 n/2 位。

例如 13 是 4 位 1101,输出应该是 L = 3 (11) 和 R = 1 (01) 在 C 中更可取(甚至伪代码也可以)

最佳答案

请看下面的伪代码。这适用于 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]#

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

关于c - 将输入位串分成两半的优雅方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20757395/

相关文章:

java - 搜索 SortedSet<String>

c - 在右边和左边用 1 填充位表示

MYSQL 搜索表、位字段

c - 搜索二进制数

c - ld.so 中是否有任何宏或指针可以给我所有 plt 部分的地址范围?

C - 如何操作 typedef 结构指针?

c - 关于输入回车键

c++ - 从 C++ 文件调用 zlib(C 代码)

python:计算句子中的单词标记

Python 。如何摆脱字符串中的 '\r'?