Skip to content

Instantly share code, notes, and snippets.

@crabshank
Last active September 3, 2023 16:28
Show Gist options
  • Select an option

  • Save crabshank/e94664e4b9be34599b6b6db1a06ff42e to your computer and use it in GitHub Desktop.

Select an option

Save crabshank/e94664e4b9be34599b6b6db1a06ff42e to your computer and use it in GitHub Desktop.
Adjust bit ranges in specified aobs to set these ranges to unsigned decimal numbers.
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
void adjBits(unsigned char* aob, unsigned long long nw,size_t st,size_t ed){
double st_d=(double)st;
//double ed_d=(double)ed;
size_t bc=(size_t)( ceil((st_d-1)/8.0));
unsigned char bbit=0;
for(size_t k=st-1;k<ed;k++)
{
bbit=(k)%8;
if(bbit==0){
bc+=1;
}
char bit= (nw >> (k-st+1)) & 1U;
aob[bc-1]^=(-bit ^ aob[bc-1]) & ( 1UL <<bbit);
//printf("%d, %d, %d\n",bc-1,bbit,bit);
}
//printf("\n");
}
int main(){
unsigned char aob[]={0x1D,0x80,0x1E,0x0};
adjBits(aob, 42,1,14); // change to 42: 1st to 14th
//printf("\n");
adjBits(aob, 43,15,32); // change to 43: next 18 bits (32-15+1=18)
//printf("\n");
for (size_t i = 0; i < sizeof(aob); ++i){
// ith byte is aob[i]
printf("%02X ", aob[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment