簡體   English   中英

C字符串和按位運算符

[英]C strings and bitwise operators

所以我有一個輸入文件,其中包含:

1 2 288815 1 13 60980

1 6 257684 1 8 250730

0 2 468583 0 0 61388

1 6 210352 0 3 23664

0 0 358489 1 13 219326

0 0 9676 0 3 402661

0 3 280447 0 3 288153

1 7 4957 0 0 397725

此信息已逐行讀入數組。

每行上的第一和第四數字是有效的支票,第二和第五數字是標簽,第三和第六數字是地址。

我需要創建一個帶有三個參數的函數:myArray,index,tag

函數將檢查參數給定索引處的行。 首先將檢查行中是否有一個與參數中的標簽相等的標簽。 如果是,則它將檢查有效數字是1還是0。如果是1,則應返回標記后的地址。 否則,它應該返回“頁面錯誤”。

這是我的第一把刀:

char *lookUpTLB(char **array, int TLBI, int TLBT)
{
if (array[TLBI][2] == TLBT)// checks if the second number in the array is equal to the tag
{
    //char **ar = array[TLBI] + 2;
    if (array[TLBI][0] == '1')//checks the valid number
    {
        return array[TLBI]+11;
    }
    else
    {
        return "Page Fault";
    }
}
else if (array[TLBI][13] == TLBT)//checks if the 13th index is equal to the tag
{
    if (array[TLBI][11] == '1')//checks the valid number
    {
        return array[TLBI] + 15;
    }
    else
    {
        return "Page Fault";
    }
}
else
{
    return "Page Fault";
}
}

該代碼幾乎沒有問題,但主要思想在那里。 我不能像我的代碼那樣嘗試使用索引,因為並非所有數字的長度都相同。 有人告訴我可以使用按位運算符來執行此操作,但是我對如何執行此操作感到非常困惑。

這是我想做的一個例子:

調用lookUpTLB(myArray,0,2); 應該返回288815

調用lookUpTLB(myArray,0,13); 應該返回60980

調用lookUpTLB(myArray,3,3); 應該返回頁面錯誤,因為有效數字為0

調用lookUpTLB(myArray,3,6); 應該返回210352

我是C語言的新手,我應該如何實現此功能? 讓我知道是否需要更清楚。

,似乎他應該轉換為數字,如果不是固定長度,只需格式化文件即可。

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>

typedef struct data {
    char valid;
    int tag;
    int32_t address;
} Data;

typedef struct rec {
    Data data[2];
} Record;

int32_t lookUpTLB(Record *array, int index, int tag);

int main(void){
    Record array[16];
    int n=0;//number of record
    FILE *fp = fopen("data.txt", "r");
    while(6==fscanf(fp, " %c %d %" SCNd32 " %c %d %" SCNd32 ,
        &array[n].data[0].valid, &array[n].data[0].tag, &array[n].data[0].address,
        &array[n].data[1].valid, &array[n].data[1].tag, &array[n].data[1].address))
    {
        ++n;
    }
    fclose(fp);
    int index, tag;
    int32_t address;
    while(1){
        printf("index(-1.:end) and tag\n");
        if(2!=scanf("%d %d", &index, &tag) || index == -1)
            break;
        if(0>(address = lookUpTLB(array, index, tag)))
            printf("Page fault\n");
        else
            printf("%" PRId32 "\n", address);
    }
    return 0;
}

#define PAGE_FAULT -1

int32_t lookUpTLB(Record *array, int TLBI, int TLBT){
    if(array[TLBI].data[0].tag == TLBT){
        return array[TLBI].data[0].valid == '1' ?
            array[TLBI].data[0].address :
            PAGE_FAULT;
    } else if(array[TLBI].data[1].tag == TLBT){
        return array[TLBI].data[1].valid == '1' ?
            array[TLBI].data[1].address :
            PAGE_FAULT;
    }
    return PAGE_FAULT;
}

演示

index(-1.:end) and tag
0 2
288815
index(-1.:end) and tag
0 13
60980
index(-1.:end) and tag
3 3
Page fault
index(-1.:end) and tag
3 6
210352
index(-1.:end) and tag
-1.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM