代码之家  ›  专栏  ›  技术社区  ›  Srikar Appalaraju Tonetel

位操作或位编程[关闭]

  •  4
  • Srikar Appalaraju Tonetel  · 技术社区  · 15 年前

    我知道逐位运算符、位操作、2的补码等概念,但当涉及到使用位操作解决问题时,我并不感到惊讶。我花时间把我的头缠在他们身上。

    我认为如果我看一些关于位运算符/位操作的问题会有所帮助,但这让我更加困惑于如何处理这个话题。我不是在寻找一个具体问题的答案,而是在处理钻头操纵时的一种普遍方法/思维方式。谢谢。

    5 回复  |  直到 15 年前
        1
  •  6
  •   Community CDub    8 年前

    到目前为止给出的答案几乎毫无用处。但是纳文给我的链接帮了我一点忙。这里举了很多例子。我正努力向他们学习。也许它能帮助别人。

    Bit Hacks

    更新: 我已经阅读了上面链接中给出的例子。他们很好。 Resource for Bitwise Programming 连接到SO。优秀的资源。在浏览了所有这些资源之后,我觉得按位编程很容易!从没想过我会用这个来形容:)

        2
  •  2
  •   High Performance Mark    15 年前

    我猜你的问题是:

    我应该采取什么样的方法 涉及位操作的问题?

    如果这是正确的,请继续阅读,如果不是,请立即停止。。。

    必须集中精力付出代价 当我通过一个 定期复习所学内容

        3
  •  1
  •   LXSoft    11 年前

    但当涉及到使用位操作来解决某个问题时,它确实做到了

    我构建了一个示例程序,用一种非常简单的方式演示了对位的操作,我从这个示例开始操作变量的某些位,并实现了使用辅助函数dec2bin(number,size,of,the,the,the,the,the,the,the,the,data)所做的更改。

    我们可以使用变量(数据)的说明性二进制部分学习非常简单的位操作。 例如,如果我们有一个变量字符(char),它包含ASCII字符“b”,使之成为大写字符“b”,我们将需要从1到0操作位号6(记住类型char有8个可用位(取决于系统体系结构)),首先想到的操作表示为c xor 0x20(因为c语言表达式将是c^=0x20);

    我们需要将第六位设置为true(小写)处理为false,将变量的内容转换为大写字符。 0x20是二进制(2)0010 0000(0)的十六进制掩码,表示0110 0010 xor 0010 0000=>0100 0010是大写字符“B”。 我们将观察到大写字符“B”异或掩码将导致小写字符“B”。

    玩这个程序我们会发现按位运算很容易理解。

    #include <stdio.h>
    #include <stdlib.h>
    
    void dec2bin(signed long long int, unsigned short size);
    
    int main()
    {
        signed long long int packedData = 0xABC4F0DE;
    
        signed long long int testData = -0xFF;
        dec2bin(testData, sizeof(signed long long int));
    
        /*
         *  NOTE:
         *  -----
         *  All printed instructions are virtually and are garbage
         *  instructions (not used anywhere in programming).
         *  That instructions are supposed to make current operation visible.
         */
        //Garbage data (random which calls for a global complex subroutine)
        printf("Istruction  1: [RND [__global__]     ] ");
        dec2bin(packedData, sizeof(unsigned long int));
    
        // NULL the data - CLR (clear all bits from data)
        // CLR is calling a sobroutine composed with AND 0x0 mask;
        packedData &= 0x0;
        printf("Istruction  2: [CLR [AND 0x0]        ] ");
        dec2bin(packedData, sizeof(signed long int));
    
        // Adding 0x3A (0011 1010) to packed data
        packedData |= 0x3A;
        printf("Istruction  3: [OR  0x3A             ] ");
        dec2bin(packedData, sizeof(signed long int));
    
        // Shift to the left this data to next nibble
        packedData <<= 4;
        printf("Istruction  4: [SHL 0x4              ] ");
        dec2bin(packedData, sizeof(signed long int));
    
        // Shift again to the left this data to next nibble
        packedData <<= 4;
        printf("Istruction  5: [SHL 0x4              ] ");
        dec2bin(packedData, sizeof(signed long int));
    
        // Adding 0xF (1111) to packed data
        packedData |= 0xF;
        printf("Istruction  6: [OR  0xF              ] ");
        dec2bin(packedData, sizeof(signed long int));
    
        // Shift again to the left this data to next byte (2 * nibble)
        packedData <<= 8;
        printf("Istruction  7: [SHL 0x8              ] ");
        dec2bin(packedData, sizeof(signed long int));
    
        // Extract contents of low ordered nibble from second byte (with a mask)
        packedData &= 0x00000F00;
        printf("Istruction  8: [AND 0x00000F00       ] ");
        dec2bin(packedData, sizeof(signed long int));
    
        // Invert (negate|NAND) each bit from data (invert mask)
        packedData = ~packedData;
        printf("Istruction  9: [INV [NOT XXXXXXXX]   ] ");
        dec2bin(packedData, sizeof(signed long int));
    
        // Shift to the right this data to previous nibble
        packedData >>= 4;
        printf("Istruction 10: [SHR 0x4              ] ");
        dec2bin(packedData, sizeof(signed long int));
    
        // Shift to the right this data to previous nibble
        packedData >>= 4;
        printf("Istruction 11: [SHR 0x4              ] ");
        dec2bin(packedData, sizeof(signed long int));
    
        // Shift to the right this data to previous nibble
        packedData >>= 2;
        printf("Istruction 12: [SHR 0x2              ] ");
        dec2bin(packedData, sizeof(signed long int));
    
        // Invert (negate|NAND) each bit from data (invert mask)
        packedData = ~(packedData) & 0x00FFFFFF;
        printf("Istruction 13: [INV [NAND 0x00FFFFFF]] ");
        dec2bin(packedData, sizeof(signed long int));
    
        // Adding 0xF0000000 (1111 0000 ... 0000) to packed data
        packedData |= 0xF0000000;
        printf("Istruction 14: [OR  0xF0000000       ] ");
        dec2bin(packedData, sizeof(signed long int));
    
        // Shift to the left this data to next nibble
        packedData <<= 4;
        printf("Istruction 15: [SHL 0x4              ] ");
        dec2bin(packedData, sizeof(signed long int));
    
        // Exclusive or
        packedData ^= 0x0F0000F0;
        printf("Istruction 16: [XOR 0x0F0000F0       ] ");
        dec2bin(packedData, sizeof(signed long int));
    
        return 0;
    }
    
    void dec2bin(signed long long int number, unsigned short size)
    {
        int c, k;
        for (c = (size*8)-1; c >= 0; c--)
        {
            k = number >> c;
            if (k & 1)
                printf("1");
            else
                printf("0");
            if (c % 4 == 0)
                printf(" ");
        }
        printf("\n");
    }
    
        4
  •  0
  •   Younes    15 年前

    所以你到底在找什么,老实说,这看起来有点模糊。你读过一本关于C的书吗?您可以查阅一些代码示例,了解如何使用C语言处理一些标准编程解决方案。

        5
  •  0
  •   Bart van Heukelom    15 年前

    我通过编写自己的紧凑、跨平台的二进制协议,在流上发送对象消息(网络套接字)。