Question: Write a function in C that begins: int rotate_left (unsigned num, int n) /*----------PSEUDOCODE----------------------*/ int rotate_left (unsigned num, int n) find the number of bytes

Write a function in C that begins:

int rotate_left (unsigned num, int n)

/*----------PSEUDOCODE----------------------*/

int rotate_left (unsigned num, int n)

find the number of bytes in an unsigned word and change it to number of bits.

create the mask with a 1 in the left-most position

for loop thru the number-of-bits to shift left

Save the left bit in variable bit. ( line in bit_print)

Left shift the num by one

Add the isolated bit in bit variable onto the right of num

[This can be done with a (1) +, (2) | , or (3) |= ]

//end for-loop

return num

/*-------------------------------------------------------------*/

{ This function should left-shift num by n positions, where the high-order bits are reintroduced as the low-order bits.

Here are two examples of a circular shift operation using a short bit pattern, rather than a full integer.

1000 0001 circular shift 1 yields 0000 0011

0110 1011 circular shift 3 yields 0101 1011

INPUT/OUTPUT DESCRIPTION: The input: in a loop, request two unsigned numbers. The output is printed to the screen by main.

FILE lab8.c

/*--------------------------------------------------------------*/

#include

#include

/* function prototypes */

void bitprint (unsigned num);

int rotate_left(unsigned num, int n);

/*-----------------------------------------*/

int main (void)

{

int left_count;

unsigned num; /* the starting number */

unsigned shifted_num;

printf(" Your Name. Lab 8. ");

do

{

/* read a unsigned integer */

printf(" Enter an unsigned integer value (0 to stop): ");

scanf("%u", &num);

if (num != 0)

{

printf(" Enter an integer value for the left shift: ");

scanf("%d", &left_count);

printf(" Original is %u ", num);

bitprint(num);

shifted_num = rotate_left(num, left_count);

bitprint(shifted_num);

printf("Shifted it is %u ", shifted_num);

}

} while (num != 0);

printf(" ");

return EXIT_SUCCESS;

}

/*--------------------------------------------------------------*/

void bitprint (unsigned num)

{

unsigned mask;

int bit, count, nbits;

/* determine the word size in bits and set the initial mask */

nbits = 8 * sizeof(unsigned); /* finds number of bytes in an unsigned w

number and changes it to bits */

mask = 0x1 << (nbits - 1); /* place 1 in left most position

starting place for the mask */

for(count = 1; count <= nbits; count++)

{

bit = (num & mask) ? 1: 0; /* set display bit on or off */

printf("%x", bit); /* print display bit */

if(count %4 == 0)

printf(" "); /* blank space after every 4th digit */

mask >>= 1; /* shift mask 1 position to the right */

}

printf(" ");

return;

}

/*--------------------------------------------------------------*/

int rotate_left(unsigned num, int n)

{

int count, bit, nbits;

unsigned mask;

nbits = 8 * sizeof(unsigned); /* finds number of bytes in an int

unsigned number and changes it to bits */

mask = 0x1 << (nbits - 1); /* place 1 in left most position

starting place for the mask */

// put the loop here and then the return

}

/*--------------------------------------------------------------*/

end file

A SAMPLE RUN:

Your Name. Lab 8.

Enter an unsigned integer value (0 to stop): 3

Enter an integer value for the left shift: 4

Original is 3

0000 0000 0000 0000 0000 0000 0000 0011

0000 0000 0000 0000 0000 0000 0011 0000

Shifted it is 48

Enter an unsigned integer value (0 to stop): 5

Enter an integer value for the left shift: 3

Original is 5

0000 0000 0000 0000 0000 0000 0000 0101

0000 0000 0000 0000 0000 0000 0010 1000

Shifted it is 40

Enter an unsigned integer value (0 to stop): 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!