2010-09-23 52 views
4

我知道位運算符,位操作,2的補等的概念,但是,當涉及到使用位處理解決的東西它不打我。我花了我的時間把頭繞在他們身上。位操作或按位編程

我想如果我看了關於位運算符/位操作的一些問題,這將有助於但它留給我更加困惑,如何處理這個問題。我不是在尋找對特定問題的答案,而是在解決位操作時尋找一種廣義的方法/思維方式。謝謝。

+0

我想說具體的問題需要具體的方法... – EboMike 2010-09-23 06:41:00

+0

他們在面試中問這些問題嗎?如果是,哪些? – Dann 2010-09-23 06:42:00

+2

你可以看看這個列表中的一些有趣的位操作應用:http://www.cs.utk.edu/~vose/c-stuff/bithacks.html – Naveen 2010-09-23 06:43:15

回答

6

答案是隔靴搔癢有用。但納文給出的鏈接對我有點幫助。這裏給出了很多例子。我正試圖向他們學習。也許它會幫助別人。

Bit Hacks

更新:我已經經歷在上面的鏈接中給出的例子。他們很好。 另外我偶然發現 - SO-Resource for Bitwise Programming鏈接。優秀的資源。在經歷了所有這些資源之後,我覺得按位編程很容易!從來沒有想過我會用在一個句子:)

+1

看起來您的鏈接頁面已重新定位。故宮。該網站鏈接到這個:[http://graphics.stanford.edu/~seander/bithacks.html](http://graphics.stanford.edu/~seander/bithacks.html) – 2010-10-06 11:20:55

+0

@Jeff。 Yupp!謝謝。也更新了帖子! – 2010-10-06 11:24:25

0

那麼究竟是你在尋找什麼,這看起來有點模糊,我是誠實的。你有沒有讀過關於例如C的書?您可以查看一些代碼示例,瞭解如何在C語言中使用某些標準編程解決方案。

2

我占卜你的問題是:

我應該採取什麼辦法,和處理涉及的位操作 問題的時候什麼 心態我應該採取,?

如果這是正確的,請繼續閱讀,如果沒有,現在停止...

位操作是對初學者很難話題 如我。我將 必須專心努力,並付出 注意,因爲我通過 分級樣本問題集。我會 修改我在常規 區間學習。目前給出

0

我學到了很多關於這個寫我自己的緊湊型,跨平臺的二進制協議,用以通過流發送對象的消息(如吮吸網絡套接字)。

1

但是,當涉及到使用位處理解決的東西它並 打我

「想想一個C變量作爲二進制字符串,並且數據通過這個二進制字符串來表示」

我構建了一個示例程序,以非常簡單的方式說明了對位的操作,我從這個示例開始操作變量的某些位,並實現用輔助函數dec2bin(number,size_of_the_data)所做的更改。

我們可以學到很簡單,用變量(數據)的二進制說明部分位操作。 例如,如果我們有一個變量字符(char),其中包含ASCII字符'b'以形成大寫字母'B',我們將需要操縱位數6(記住char類型有8位可用(取決於系統架構))從1到0,第一個記住操作表示爲c xor 0x20(對於C語言表達式將是c^= 0x20);

說明:

b - 0110 0010 - 大寫乙 - 0100 0010(如何?)

我們需要處理位排名第六,其設置爲true(小寫)至將會將變量的內容轉換爲大寫字符的錯誤。 查看真值表AND,OR,XOR,不是我們選擇的真值表是XOR真值表,因爲邏輯理論屬性1 xor 1導致0位值,在C中這個操作表示爲^。 什麼是0x20是二進制(2)0010 0000(0)中的十六進制掩碼,該表達式表示0110 0010 xor 0010 0000 => 0100 0010是大寫字符'B'。 我們將觀察到大寫字母'B'xor掩碼將導致小寫字符'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"); 
}