static inline void segmentalloc(int total_size, int request) { int i, flevel = 0, size = total_size; if (total_size < request) { puts(" * Result:"); puts(" * The system don't have enough free memory"); puts(" * Suggession : Go for VIRTUAL MEMORY"); return; } while (1) { if (request <= size && div2(size) < request) { break; } else { size = div2(size); flevel++; } } for (i = power_2(flevel) - 1; i <= (power_2(flevel + 1) - 2); i++) { if (tree[i] == 0 && place(i)) { tree[i] = request; makedivided(i); puts(" * Result : Successful Allocation"); break; } } if (i == power_2(flevel + 1) - 1) { puts(" * Result :"); puts(" * The system don't have enough free memory"); puts(" * Suggession : Go for VIRTUAL Memory Mode"); } }
static inline void printing(int total_size, int node) { int permission = 0, llimit, ulimit, tab; if (node == 0) { permission = 1; } else if (mod2(node) == 0) { permission = (tree[div2(node - 1)] == 1) ? 1 : 0; } else { permission = (tree[div2(node)] == 1) ? 1 : 0; } if (permission) { llimit = ulimit = tab = 0; while (!(llimit <= node && node <= ulimit)) { tab++; putchar('\t'); llimit = ulimit + 1; ulimit = 2 * ulimit + 2; } printf(" %d ", total_size / power_2(tab)); if (1 < tree[node]) { printf("---> Allocated %d\n", tree[node]); } else if (tree[node] == 1) { printf("---> Divided\n"); } else { printf("---> Free\n"); } printing(total_size, 2 * node + 1); printing(total_size, 2 * node + 2); } }
int main(int argc, char *argv[]) { printf("%f\n", power_0(4, 10)); printf("%f\n", power_0(4, 11)); printf("%f\n", power_1(4, 10)); printf("%f\n", power_1(4, 11)); printf("%f\n", power_2(4, 10, 1)); printf("%f\n", power_2(4, 11, 1)); printf("%f\n", power_3(4, 10); printf("%f\n", power_3(4, 11)); return 0; }
double power_2(int x, int n, double r) { if (n == 0) return r; if (n == 1) return r * x; if (n % 2 != 0) r *= x; return power_2(x * x, n / 2, r); }
int main(){ //taking input in hex char * hex = (char *)malloc(sizeof(char)* 20); scanf("%s", hex); printf("%s\n", hex); int a; sscanf(hex,"%x",&a); int input_length = no_of_bits(a); print_binary(a); // printf("%d\n", r(a)); int nbits = no_of_bits(a); int rval = r(nbits); printf("rval is%d\n",rval); int msg = power_2(rval+nbits)- 1; int msg_length = rval+nbits; int i; int p_index = 0; int p; while(1){ p = power_2(p_index); if (p >= (msg_length)) break; unset_bit(&msg, p,(msg_length)); printf("%d\n",p); p_index++; } printf("After setting the parity bits to 0\n"); print_binary(msg); int j = 1; for(i = 1; i<= input_length; i++){ while(check_bit_left(msg, j,msg_length) == 0) j++; printf("j is %d\n", j); if(check_bit_left(a, i, input_length)) set_bit(&msg, j, msg_length); else unset_bit(&msg, j, msg_length); j++; } printf("After setting the input bits\n"); print_binary(msg); printf("Checking bit 1 %d\n",check_bit_left(msg,1, msg_length) ); p_index = 0; while(1){ p = power_2(p_index); if (p > (msg_length)) break; printf("p is %d\n", p); int x= p; int c; int count= 0;//count for 1's in bit representaion while(1){ // printf("x is %d\n", x); if(x > msg_length) break; for(c = 0; c <p;c++){ if(x > msg_length) break; printf("Checking %d\n", x); if (check_bit_left(msg,x, msg_length)) count++; x++; } for(c = 0; c<p;c++){ x++; } } printf("Count for p = %d is %d \n",p, count ); if (count %2 != 0){// if odd, need to add 1 to set it for even parity set_bit(&msg, p, msg_length); } // unset_bit(&msg, p,(msg_length)); // printf("%d\n",p); p_index++; } printf("After correcting the parity bits\n"); print_binary(msg); printf("THis is the hamming code you have to send\n"); printf("%x\n", msg); int e; // printf("Enter a sample bit position to change to test for error correction\n"); // scanf("%d", &e); e = 7; int sample_error = msg; if (check_bit_left(sample_error, e, msg_length)) unset_bit(&sample_error, e, msg_length); else set_bit(&sample_error, e, msg_length); printf("Before correcting error\n"); print_binary(sample_error); p_index = 0; int error_index = 0; while(1){ p = power_2(p_index); if (p > (msg_length)) break; // printf("p is %d\n", p); int x= p; int c; int count= 0;//count for 1's in bit representaion while(1){ // printf("x is %d\n", x); if(x > msg_length) break; for(c = 0; c <p;c++){ if(x > msg_length) break; // printf("Checking %d\n", x); if (check_bit_left(sample_error,x, msg_length)) count++; x++; } for(c = 0; c<p;c++){ x++; } } // printf("Count for p = %d is %d \n",p, count ); if (count %2 != 0){// if odd, need to add 1 to set it for even parity error_index += p; } // unset_bit(&msg, p,(msg_length)); // printf("%d\n",p); p_index++; } if (check_bit_left(sample_error, error_index, msg_length)) unset_bit(&sample_error, error_index, msg_length); else set_bit(&sample_error, error_index, msg_length); printf("After correcting error\n"); print_binary(sample_error); printf("error index is %d\n", error_index); }