简体   繁体   English

C字符串和按位运算符

[英]C strings and bitwise operators

So I have a input file which contains: 所以我有一个输入文件,其中包含:

1 2 288815 1 13 60980 1 2 288815 1 13 60980

1 6 257684 1 8 250730 1 6 257684 1 8 250730

0 2 468583 0 0 61388 0 2 468583 0 0 61388

1 6 210352 0 3 23664 1 6 210352 0 3 23664

0 0 358489 1 13 219326 0 0 358489 1 13 219326

0 0 9676 0 3 402661 0 0 9676 0 3 402661

0 3 280447 0 3 288153 0 3 280447 0 3 288153

1 7 4957 0 0 397725 1 7 4957 0 0 397725

This information has been read in line by line into an array. 此信息已逐行读入数组。

The first and fourth numbers on each line are a valid check, the second and fifth numbers are a tag, and the third and sixth numbers are an address. 每行上的第一和第四数字是有效的支票,第二和第五数字是标签,第三和第六数字是地址。

I need to create a function which takes three arguments: myArray, index, tag 我需要创建一个带有三个参数的函数:myArray,index,tag

The function will check the line at the index given in the parameter. 函数将检查参数给定索引处的行。 It will first check to see if there is a tag in the line which is equal to the tag in the parameter. 首先将检查行中是否有一个与参数中的标签相等的标签。 If it is, then it will check if the valid number is a 1 or a 0. If it is a 1, then it should return the address after the tag. 如果是,则它将检查有效数字是1还是0。如果是1,则应返回标记后的地址。 Else, it should return "Page fault". 否则,它应该返回“页面错误”。

This is my first stab at it: 这是我的第一把刀:

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";
}
}

There are few problems with this code, but the main idea is there. 该代码几乎没有问题,但主要思想在那里。 I can't use indexing like a tried to with my code because not all the numbers are the same length. 我不能像我的代码那样尝试使用索引,因为并非所有数字的长度都相同。 I've been told I can do this using bitwise operators, but I'm very confused on how to do that. 有人告诉我可以使用按位运算符来执行此操作,但是我对如何执行此操作感到非常困惑。

Here is what I want to happen as an example: 这是我想做的一个例子:

Calling lookUpTLB(myArray, 0, 2); 调用lookUpTLB(myArray,0,2); should return 288815 应该返回288815

Calling lookUpTLB(myArray, 0, 13); 调用lookUpTLB(myArray,0,13); should return 60980 应该返回60980

Calling lookUpTLB(myArray, 3, 3); 调用lookUpTLB(myArray,3,3); should return page fault because the valid number is 0 应该返回页面错误,因为有效数字为0

Calling lookUpTLB(myArray, 3, 6); 调用lookUpTLB(myArray,3,6); should return 210352 应该返回210352

I'm pretty new to C, any help on how I should do this function? 我是C语言的新手,我应该如何实现此功能? Let me know if I need to be more clear. 让我知道是否需要更清楚。

,It seems that he should be converted to a number simply format the file if not a fixed length. ,似乎他应该转换为数字,如果不是固定长度,只需格式化文件即可。

#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;
}

DEMO : 演示

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