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。 是否与万国主义相差错?
而且,除了使用特性阵列之外,它还有更好的选择?