我知道位运算符、位操作、二进制补码等概念。但是在使用位操作解决问题时,我并不容易想到它们。我需要花费时间来理解它们。
我认为查看一些关于位运算符/位操作的问题可能会有所帮助,但是这让我更加困惑如何处理这个主题。我不是在寻找特定问题的答案,而是在处理位操作时寻找一般化的方法/思路。谢谢。
我认为查看一些关于位运算符/位操作的问题可能会有所帮助,但是这让我更加困惑如何处理这个主题。我不是在寻找特定问题的答案,而是在处理位操作时寻找一般化的方法/思路。谢谢。
我猜测您的问题是:
在处理位运算问题时,我应该采取什么方法和思维方式?
如果我猜对了,请继续阅读,否则请停止...
对于像我这样的初学者来说,位运算是一个困难的主题。我需要集中精力,仔细注意,并通过一组分级示例问题来学习。我会定期复习所学内容。
#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");
}
那么你具体是在寻找什么呢?说实话,这看起来有点模糊。你是否读过一本关于C语言的书籍?或许你可以查阅一些代码示例,了解如何在C语言中处理一些标准编程解决方案。
通过编写自己的紧凑、跨平台的二进制协议来发送对象消息,我学到了很多关于这方面的知识(例如网络套接字)。