【问题标题】:Bitwise Operators on a binary representation in C [closed]C中二进制表示的位运算符[关闭]
【发布时间】:2014-06-25 00:21:59
【问题描述】:

我有一个数字的二进制表示形式为最多 18 位长的 char 数组。我需要使用位运算符将右侧的三位存储到两个不同的字符串中。

例如,我有一个包含“00000000011101”的字符数组。我需要一个函数来存储“101”(最后三位)并存储“00000000011”(其余位)。

但是,这必须使用位运算符来完成。所以我知道我需要将字符串转换为 int,然后转换,但我不知道该怎么做。

非常感谢您的帮助。

【问题讨论】:

  • “这是我的问题;为我解决” 嗯,不。展示你的工作,描述你的困难,并提出一个可以回答的问题
  • 如果您已经将它存储在字符数组中,那么按位运算符并不是特别有用。只需找出提取适当子字符串所需的索引...
  • @twalberg:我怀疑使用按位运算是家庭作业的要求。
  • 为什么你总是在回答完问题内容后删除?

标签: c binary bitwise-operators


【解决方案1】:

将字符串转换为int 很容易:从零int 开始,然后遍历二进制表示的字符,并检查它是零还是一。首先,将部分结果向左移动。然后,如果数字为 1,则结果为 1。继续下一个字符。当你看到空终止符'\0' 时,停止。

这是一些伪代码:

res = 0
ptr = "00000000011101"
while ptr is not pointing to '\0'
    res <<= 1;
    if ptr points to '1'
        res |= 1;

一旦你有了你的int,获取最后三位就相当于将&amp; 与七的结果相与(其二进制表示为0000111)。获取其余位相当于将&gt;&gt; 数字右移三位。

这是一些伪代码:

lastThree = res & 7
topBits = res >> 3

【讨论】:

  • hmm 可以用伪代码给我看一个例子吗?我是位运算符和 c 的新手。
【解决方案2】:

你总是可以自己做。其实很简单。

int BitwiseOr(char str1[], char str2[]) //Although it probably works on not whole numbers too.
{
    int i, output = 0;
    char* outputstr = malloc(sizeof(char)(strlen(str1)>strlen(str2)?strlen(str1):strlen(str2))); //Although size of char is mostly 1. It's defined to fit to the biggest string size (binary representation there is).
    char* rstr1;
    char* rstr2;
    if(strlen(str1)==strlen(str2)
    {
        rstr1 = str1; //Point to the same address.
        rstr2 = str2; //As we are going to use these variables for cases if the strings' length aren't equal and we need to add zeros to the left.
    }
    else if(strlen(str1)>strlen(str2)
    {
        rstr1 = str1;
        rstr2 = malloc(sizeof(char)*strlen(str1));
        for(i = 0 ; i < strlen(str1)-strlen(str2) ; i++)
        {
            rstr2[i] = '0';
        }
        for(i = i /*To be one spot after the first loop was at*/ ; i < strlen(str1) ; i++)
        {
            rstr2[i] = str1[i];
        }
    }
    else
    {
        rstr1 = malloc(sizeof(char)*strlen(str2));
        rstr2 = str2;
        for(i = 0 ; i < strlen(str2)-strlen(str1) ; i++)
        {
            rstr1[i] = '0';
        }
        for(i = i /*To be one spot after the first loop was at*/ ; i < strlen(str1) ; i++)
        {
            rstr1[i] = str1[i];
        }
    }
    /*After memory is allocated*/
    for(i = 0 ; i < strlen(outputstr) ; i++)
    {
        outputstr[i] = (str1[i]-'0') || (str2[i]-'0');
    }
    /*Now turn the number from binary string to decimal - pretty simple. Just go from the right to the left with powers of 2 when the first is 2 powered by zero and so on and when it's '1' in the output string - add to the output integer the 2 powered by the spot from the right*/

    return output;
}

对位运算符和用途的一些解释。 位运算符实际上使用数字的二进制表示并使用另一个数字对其执行操作。示例是 OR,如上所述。获取所有位,然后将它们全部或得到一个数字。它有什么用?其实很多东西。特别是在处理图形和颜色时,OpenGL 在设置窗口时使用它们,它定义常量并发送它们的按位 OR 解决方案 - 这样它就可以通过知道可能的选项来了解您选择的所有选项。

【讨论】:

    【解决方案3】:

    这里有一个可用的 C 程序来演示一个可能的解决方案:

    #include <stdio.h>
    #include <string.h>
    
    main()
    {
        int i;
        unsigned int res = 0;
        unsigned int bitmask = 1;
        static unsigned char *ptr = "00000000011101";
    
        printf("ptr = %s\n", ptr);
        for (i=(strlen(ptr) -1); i>0; i--) {
            printf("ptr[%d] = %c (bitmask = 0x%0x)\n", i, ptr[i], bitmask);
            if (ptr[i] == '1')
                res = (res | bitmask);
            bitmask <<= 1;
        }
        printf("res = 0x%x\n", res);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 2013-06-23
      相关资源
      最近更新 更多