Write a function called circularRightShift(int var, int n)

advertisement
OOP444 C D E (circle one)
Test one (2)
Name:
Surname:
Student number:
1-[6 marks] Write a function called circularRightShift(int var, int n).
Rotates the bits in “var” to right, “n” times
circularRightShift() shifts the bits to right exactly like right shift, but instead of filling the
left bits with zero, it fills the left bits with the shifted-out bits of the right side.
Other words: in takes the bits from the right and puts them at left.
Notes: regardless of what the size of the integer is, this function must work correctly.
Bonus: extra 3 marks for anyone who does this without using the sizeof operator.
Example:
circularRightShift(var, 1);
var = 1101 0011 0101 1010 before
var = 0110 1001 1010 1101 after
circularRightShift(var, 4);
var = 1101 0011 0101 1010 before
var = 1010 1101 0011 0101 after
2- [6 marks] Determine the exact output of the following program if it is compiled and
the executable is called a.out and it is executed from the command line as follows:
$ a.out qxfhsd! fgtwk atudq
Hint: char (*A)[9]; A is a pointer to a two dimensional array with 9 as its second index.
Other words: if char B[whatever][9]; is a two dimensional array, then A could be a
pointer to B.
#include <stdio.h>
char Get(char in[],int i){
if(i%2){
return --in[i];
}
else{
return ++in[6-i];
}
}
void main(int argc, char *argv[]){
char
x[20]="";
char (*n)[9] = (char (*)[9])x;
int
k;
for(;argc-1;argc--){
strcat(x,argv[argc-1]);
}
for(k=0;k<6;k++){
printf("%c",Get(n[!(k%3)],k));
}
printf("R!\n");
}
3- [5 marks] Determine the exact output of the following program:
/* remeber: printf returns number of characters it prints
pre-processor directives will be processed
before compilation
*/
#include <stdio.h>
#define Define(a,b) b a
#define Set(a,b) a=b
#if defined(NULL)
#define FUN(a) printf("**%s**\n",a + 15 )
#else
#define FUNNY(a) a+=10
#endif
int main(void){
Define(p,char*);
Define(str[],char) = "abcdefghijklmnopqrstuvwxyz";
Set(p,str);
#ifdef FUN
FUN(str);
#else
printf("%s\n",FUNNY(p));
#endif
#ifndef FUN
#undef FUNNY
#define FUNNY(a) a+=14
#define FUN(a) (a*2+a+5)
*(p+25) = !p;
#else
#undef FUN
#define FUNNY(a) a++
#define FUN(a) (a+a*6)
*(p+4) = (p == NULL);
#endif
FUNNY(p);
printf("%d", FUN(printf(p)));
return 0;
}
6-[5 marks] Write a function called decrypt.
void decrypt (void *out, void *in, long size, void (*dcrpt)(char*, char));
This function decrypts any “size” of date passed to it through “in”. It receive the data
from “in” and then decrypts it byte by byte and copies the decrypted version of “in” into
“out”. The core of decryption is done by a function with the following prototype:
void decrytpByte(char *byteout, char bytein)
This function receives a char and passes back its decrypted version. The “decrypt”
function that you are writing will apply this type of function to decrypt the data.
Download