I need to fill in the missing pieces of code I will state the purpose before the
ID: 672917 • Letter: I
Question
I need to fill in the missing pieces of code I will state the purpose before the void. Here's what I have:
#include <stdlib.h>
#include <stdio.h>
const char ZERO = 0;
const char ONE = 1;
#define TWO (ONE + ONE)
const char NIBBLE_WITH_ALL_BITS_ON = 0xF;
const int BYTE_WITH_ALL_BITS_ON = 0xFF;
const int NUM_BITS_PER_NIBBLE = 4;
const int NUM_BITS_PER_BYTE = 8;
const int NUM_NIBBLES_PER_BYTE = 2;
const int NUM_BYTES_PER_WORD = sizeof(int);
#define NUM_NIBBLES_PER_WORD (NUM_BYTES_PER_WORD * NUM_NIBBLES_PER_BYTE)
#define NUM_BITS_PER_WORD (NUM_BYTES_PER_WORD * NUM_BITS_PER_BYTE)
#define NUM_BITS_PER_WORD_MINUS_ONE (NUM_BITS_PER_WORD - ONE)
#define NUM_BITS_PER_BYTE_PAIR (NUM_BITS_PER_BYTE * 2)
const int TEXT_LEN = 64;
// Here's part 1:
// Part 2
// I think this part is already written but I'm not sure so some feedback would be nice
// Part 4
// Here's my main so far
Any help is much appreciated!!!
Explanation / Answer
Program:
#include <stdlib.h>
#include <stdio.h>
const char ZERO = 0;
const char> #define TWO (ONE + ONE)
const char NIBBLE_WITH_ALL_BITS_ON = 0xF;
const int BYTE_WITH_ALL_BITS_ON = 0xFF;
const int NUM_BITS_PER_NIBBLE = 4;
const int NUM_BITS_PER_BYTE = 8;
const int NUM_NIBBLES_PER_BYTE = 2;
const int NUM_BYTES_PER_WORD = sizeof(int);
#define NUM_NIBBLES_PER_WORD (NUM_BYTES_PER_WORD * NUM_NIBBLES_PER_BYTE)
#define NUM_BITS_PER_WORD (NUM_BYTES_PER_WORD * NUM_BITS_PER_BYTE)
#define NUM_BITS_PER_WORD_MINUS_ONE (NUM_BITS_PER_WORD - ONE)
#define NUM_BITS_PER_BYTE_PAIR (NUM_BITS_PER_BYTE * 2)
const int TEXT_LEN = 64;
// PURPOSE: To ask for and get a hexadecimal integer. Keeps asking until
// gets legal hexadecimal integer. Returns the integer. No parameters.
unsigned int getHexadecimalInt()
{
char text[TEXT_LEN];
unsigned int u;
int numRead;
do
{
printf(" Please enter a hexadecimal int (without the leading "0x"): ");
fgets(text,TEXT_LEN,stdin); // reads the hexadecimal number from user as text
numRead = sscanf(text,"%X",&u); //Read hexadecimal data into u from text and set numRead to 1 upon success
}
while (numRead < ONE); // to repeat loop until u is read properly
return(u);
}
// PURPOSE: To print 'u' both in binary and in hexadecimal, followed by a
// newline character. No return value.
void printBinaryAndHex (unsigned int u)
{
int place;
// HINT: (hexadecimal)
// Use format 0x%08X.
printf(" Hexadecimal format: ");
printf("0x%08X ",u); // printing in hexa decimal
// HINT: (binary)
// Make an integer with its highest bit on. Compare it with 'u'.
// If the corresponding bit is on in 'u' then print '1', else print '0'.
// Now move the bit over to the next lower bit position.
printf(" Binary format: ");
for(place=(NUM_BYTES_PER_WORD*8)-1; place>=0; place--)
(u&(1<<place))?putchar('1'):putchar('0');
printf(" ");
}
// PURPOSE: To output a sequence of integers where a ONE in the lowest bit
// "slides" to the highest bit position. Then another ONE in the lowest
// bit "slides" to the *second* highest bit position, etc. The bits
// should be kept on when then reach their final position. No parameters.
// No return value.
void bitSlider()
{
unsigned int u0;
unsigned int uAccumulator = ZERO;
// HINT:
// Have 2 loops.
// The outer loop will go from
// 0x8000,0000 to 0x4000,0000 to 0x2000,0000 to ... 0x0000,0001
// The inner loop will go from ONE to the index of the outer loop.
}
// PURPOSE: To repeatedly ask for a 32-bit hexadecimal integer. If the
// number is not ZERO then the integer is separated into the highest 16
// bits and the lowest 16 bits. The highest 16 bits are repeatedly rolled
// to the left 16 times, while the lowest 16 bits are repeatedly rolled
// to the right 16 times. After each roll, the number is reconstituted
// and printed.
void rollHighBitsLeft_rollLowBitsRight()
{
unsigned int uChoice=0;
unsigned int halfBitsOfWord = NUM_BITS_PER_WORD / TWO;
unsigned int loMask = (BYTE_WITH_ALL_BITS_ON << ZERO) |
(BYTE_WITH_ALL_BITS_ON << NUM_BITS_PER_BYTE);
unsigned int hiMask = loMask << halfBitsOfWord;
unsigned int maskForRollLeft = ONE << (halfBitsOfWord - ONE);
unsigned int maskForRollRight= ONE;
unsigned int high;
unsigned int low;
int i,j;
while ( (uChoice = getHexadecimalInt()) != ZERO )
{
printBinaryAndHex (uChoice) ;
high=uChoice & hiMask;
low=uChoice&loMask;
// HINT:
// Have 2 loops.
// The outer loop will iterate about halfBitsOfWord times.
for(i=0;i<halfBitsOfWord;i++)
{
high=(high&maskForRollLeft);
// The inner loop rolls by the amount given in the outer loop.
for(j=i;j<halfBitsOfWord;j++)
{
low=low&maskForRollRight;
}
uChoice=high|low;
printBinaryAndHex (uChoice) ;
}
}
}
int main()
{
rollHighBitsLeft_rollLowBitsRight();
//bitSlider();
return 0;
}