English 中文(简体)
C:这部法典是否适用于左轮改变特性阵列,取代“nth”轨,并根据情况将第0轨定为0或1;
原标题:C: Is this code snippet correct for left shifting a character array, resting the "nth" bit and setting the 0th bit as 0 or 1 based on a condition?
  • 时间:2011-10-22 19:09:40
  •  标签:
  • c

I have to do this program where i have to use a register which can be anything between 1 to 20 bits long. i could only think of using a character array for this purpose. I want to left shift(by 1), reset the nth bit and set or reset the 0th bit depending on a condition. Since i do not know the number of bits until at run time, i am using using malloc to allocate the size of the character array. Please tell me if this is correct

// history bits can change from 1 to 20
int historyRegisterSize=(historybits+7)/8;

// allocating memory dynamically
historyRegister=malloc(historyRegisterSize * sizeof(unsigned char));

// Shifting left one bit
    unsigned char *byte;
    int size=historyRegisterSize;
    for( byte =historyRegister; size--; ++byte )
    {
        unsigned char bit = 0;
        if (size>=0)
        {
            bit = byte[1] & (1 << (8 - 1)) ? 1 : 0;
        }
        *byte <<= 1;
        *byte |= bit;
    }

    // Resetting the nth bit 
    historyRegister[0]=historyRegister[0] & 0x7;

     // or should i use this one for resetting?
     //historyRegister[historyRegisterSize-1] &= ~(1 <<(historybits-1));

    // Setting the 0th bit based on a condition
    if(condition)
    {
        historyRegister[historyRegisterSize-1]=historyRegister[historyRegisterSize-1] | 1;
        // or should i use this statement below?
        //historyRegister[0] |= 1 <<0;
    }
    else
    {
    historyRegister[historyRegisterSize-1]=historyRegister[historyRegisterSize-1] & 0xfe;
      // or should i use this statement below?
      //historyRegister[0] &= ~(1 <<0);
    }

在这样做之后,我只想把64个字塔与我的特性阵列(历史重新编号)毫不相干,并作成组合。 为此,我使用本声明。

// result and var2 and unsigned long variables 
// and size is another unsigned integer.
result=(var1 ^ *(unsigned long int *)historyRegister) % size; 

一切似乎都是正确的? 我的问题是,当我改变特性阵列的比值时,产出价值在范围上似乎保持不变。 这一数字从1比8比8,8比16,16比20。 是否与万国主义相差错?

而且,除了使用特性阵列之外,它还有更好的选择?

问题回答

If you only need to store 20 bits it would be easier to use an unsigned int or long. Shifting could then be done by

val <<= 1;

你们可以躲避最左边的东西。

val &= ((1u <<n) -1);




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签