This page contains C code for Von Neumann method using Bitwise C operator and a code to obtain a shared object for R. Also one code is given to save the output of C directly in a file. Then we are accessing the file directly from R to make plots etc. The codes are as follows:
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <unistd.h> /*Programmers: Gursharn, Smruti, Shwetab, Sagnika, Kaustav*/ /*Date: 18/08/2010 */ /*Bitwise Code for Von Neumann*/ unsigned long int bitv(unsigned long int x, int n); int main(void) { int n, m, i; unsigned long int temp, x; printf("Enter the number of digits in the binary number "); scanf("%d",&n); printf("Enter the number of random numbers to be generated "); scanf("%d",&m); srand(getpid()); x =(int)((pow(2.0,n * 1.0) - 1) * (rand()/(double)RAND_MAX)); for(i = 0; i < m; i++){ printf("x = %lu",x); temp = bitv(x,n); printf("\t %lu\n",temp); x = temp; } return 0; } /*Bitwise Shift Operator*/ unsigned long int bitv(unsigned long int x, int n) { unsigned long int y, z; y = x * x; z = y >> n/2; z = z << (sizeof(unsigned long int) * 8 - n); z = z >> (sizeof(unsigned long int) * 8 - n); return z; }
#include <R.h> #include <Rdefines.h> #include <Rinternals.h> #include <stdlib.h> /*Programmers: Gursharn, Smruti, Shwetab, Sagnika, Kaustav*/ /*Date: 18/08/2010 */ /*Bitwise Code for Von Neumann*/ unsigned int bitv(unsigned int x, int n); SEXP vonbit(SEXP n, SEXP m, SEXP r) { int i; unsigned int x; int N = INTEGER_POINTER(n)[0], M = INTEGER_POINTER(m)[0]; double R = NUMERIC_POINTER(r)[0]; SEXP ans; ans = PROTECT(NEW_INTEGER(M)); int *z = INTEGER_POINTER(ans); /* unsigned long int temp, x; printf("Enter the number of digits in the binary number "); scanf("%d",&n); printf("Enter the number of random numbers to be generated "); scanf("%d",&m);*/ srand((int)(R * pow(10.0,4.0))); x =(int)((pow(2.0, N * 1.0) - 1) * (rand()/(double)RAND_MAX)); for(i = 0; i < M; i++){ z[i] = bitv(x,N); x = z[i]; } UNPROTECT(1); return ans; } /*Bitwise Shift Operator*/ unsigned int bitv(unsigned int x, int n) { unsigned int y, z; y = x * x; z = y >> n/2; z = z << (sizeof(unsigned int) * 8 - n); z = z >> (sizeof(unsigned int) * 8 - n); return z; }
Here the data is stored in a file named data
.
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <unistd.h> /*Programmers: Gursharn, Smruti, Shwetab, Sagnika, Kaustav*/ /*Date: 18/08/2010 */ /*Bitwise Code for Von Neumann*/ unsigned long int bitv(unsigned long int x, int n); int main(void) { int n, m, i; FILE *fp; unsigned long int temp, x; printf("Enter the number of digits in the binary number "); scanf("%d",&n); printf("Enter the number of random numbers to be generated "); scanf("%d",&m); fp = fopen("data","w"); srand(getpid()); x =(int)((pow(2.0,n * 1.0) - 1) * (rand()/(double)RAND_MAX)); for(i = 0; i < m; i++){ temp = bitv(x,n); fprintf(fp,"%lu",temp); x = temp; } fclose(fp); return 0; } /*Bitwise Shift Operator*/ unsigned long int bitv(unsigned long int x, int n) { unsigned long int y, z; y = x * x; z = y >> n/2; z = z << (sizeof(unsigned long int) * 8 - n); z = z >> (sizeof(unsigned long int) * 8 - n); return z; }