2014-12-03 33 views
2

我有提取二進制表示的最後4個數字 我的電話號碼的一個問題是:1111 1001 我拿到第一個4 但是我必須拿到的問題過去四年^:提取的最後四位數的INT的二進制表示

# include <stdio.h> 
# include <stdlib.h> 
# define BYTETOBINARYPATTERN "%d%d%d%d%d%d%d%d\n" 
# define BYTETOBINARY(byte) \ 
(byte & 0x80 ? 1 : 0), \ 
(byte & 0x40 ? 1 : 0), \ 
(byte & 0x20 ? 1 : 0), \ 
(byte & 0x10 ? 1 : 0), \ 
(byte & 0x08 ? 1 : 0), \ 
(byte & 0x04 ? 1 : 0), \ 
(byte & 0x02 ? 1 : 0), \ 
(byte & 0x01 ? 1 : 0) 

int main() 
{ 
    int num = 505; 
    PRINTBIN (num); 

    // int result = num & bitwise; 
    //PRINTBIN(result); 
    // first num; 
    int i; 
    int bit=0x01; 
    int bitwise; 
    for (i =0;i<4;i++) 
    { 
     int bit1=bit<<i; 
     bitwise = bit1|bit; 

    } 
    printf("1:%d\n", bitwise); 
    PRINTBIN(bitwise); 

    //second num; 
    int bitwise1; 
    int b0 = 0; 
    int bit2; 
    for(i=0;i<4;i++) 
    { 
     bit2 = bit<<(4+i); 
     bitwise1 = bit2|b0;  
     PRINTBIN(bit2); 
    } 
    printf("2:%d\n",bitwise1); 
    PRINTBIN(bitwise1); 

    return 0; 

} 

回答

2

有比你正在做的事情更簡單的方法:num & 0x0f將獲得四強,那麼你只要打印出來的二進制與printf("%04b\n", num &0x0f);。編輯:%b說明符是非標準的!可能不應該使用那個,而是使用下面的例子。 /編輯

任何f十六進制是1111在二進制,這使得它很容易拔出這些四重奏。如果你不能只使用printf,你也可以通過一次掩蓋一個而輕鬆打印,然後將數字移到另一個以顯示。例如:

for(int i = 0; i < 4; i++) { 
    printf("%c", (number & 8) ? '1' : '0'); // print the number on the left of the quartet 
    number <<= 1; // shift it to the right by one position, putting the next bit in position to print next loop around 
} 

的其他位操作有用的技巧是圍繞移位一個拿到面具。要從右邊開始#n,請執行number & (1 << n)。一個左移零時間是一個 - 這個掩碼讓你成爲最不重要的(最右)位。一個向左移動一個是兩個,第二個最小有效位,依此類推。你的bit1 = bit << i行可以做到這一點,但我更願意使用文字1來描述這個技巧,以表明它不一定是一個變量。

+0

Oooh,我認爲那是第二個四,使用更好的術語可能是最重要的,也是最不重要的。最重要的是最左邊的文字,稱爲「最重要的」,因爲它們代表了更大的數字。無論如何,我這樣做的方式是將數字右移四次:'num >> = 4;'然後按照您對第一個過程的相同方式重複該過程。通過轉移它,將新問題轉換爲現有解決的問題,以便您再次執行相同的操作。 – 2014-12-03 04:20:53

+0

這太好了。非常感謝你。我學會了一種新技術,使按位操作變得更容易 – SunnyTrinh 2014-12-03 04:23:08

+0

沒有'%b'轉換說明符(至少就C規範或POSIX而言)。 – dreamlax 2014-12-03 04:24:24

2

如果您只想顯示4位數字,只需使用查找表格,因爲我們已經知道所有可能的表示形式。 如果你想顯示其他4位在其他位置,只是做了換擋num&0x0F之後,但在面具會被其他價值,而不是0x0F

const char * bin[16] = { 
    "0000", 
    "0001", 
    "0010", 
    ...... 
    "1111" 
}; 

printf("%s\n", bin[num&0x0F]); 
0

num & 0xF將提取的持續數字。

要打印的最後4,代碼可以使用:

printf("%04d", (num&8)*125 + (num&4)*25 + (num&2)*5 + (num&1)); 

另一種方法用八進:
要打印至少顯著8,代碼可以使用:

printf("%08lo", 
    (num&128)*16384LU + (num&64)*4096L + (num&32)*1024L + (num&16)*256 + 
    (num&8)*64 + (num&4)*16 + (num&2)*4 + (num&1)); 

此方法適用於至多21位,使用多項式或for循環的unsigned long long

0
The following function extractAllBitsFromIntToArray 
Parameters: 
int value: the source integer to use for the extraction 
int* totalBits: pointer to callers' area to receive size of returned int array 
Returns: 
int* a pointer to a malloc'd array that contains the extracted bits from 'value' 
    least significant bit at offset 0 into the array. 
    note: the caller must 'free()' this array when done using it 


#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#define TOTAL_BITS (sizeof(int)<<3) // assumes 8 bit bytes 
#define BIT_MASK(count) (1<<count) 

int * extractAllBitsFromIntToArray(int value, int* numBits) 
{ 
    int i = 0; // loop counter 
    int *pRetArray = NULL; 
    if(NULL == (pRetArray = malloc(TOTAL_BITS*sizeof(int)))) // array to return 
    { 
     perror("malloc failed"); 
     exit(EXIT_FAILURE); 
    } 

    memset(pRetArray, 0x00, TOTAL_BITS*sizeof(int)); // clear array 

    // extract bits, where pRetArray[0] is LSB 
    for(i=0; i<TOTAL_BITS; i++) 
    { 
     pRetArray[i] = (value&(BIT_MASK(i)))? 1: 
    } 

    return(pRetArray); 
} // end function: extractAllBitsFromIntToArray 
+1

1)也許'#define TOTAL_BITS(sizeof(int)* CHAR_BIT)'然後不需要假設?2)建議'無符號值'和'BIT_MASK(count)(1u << count)'以避免移入int符號位。 3)在'(值&(BIT_MASK(i)))末尾丟失了什麼? 1:'? – chux 2014-12-03 06:08:54

相關問題