Purpose: To let you demonstrate your ability to manipulate C integers. Assignmen
ID: 671879 • Letter: P
Question
Purpose:
To let you demonstrate your ability to manipulate C integers.
Assignment
Finish the program below that does several bit-wise manipulations of C integers.
Cut and paste the following:
Finish the Program!
You may only use the integer constants given at the beginning. (You may use whatever integers you wish in string constants as long as they are for printf()'s. For example the format string "0x%08X" is useful for printing hexadecimal integers.)
You may only use the following integer operators:
+
-
++
--
<<
>>
~
|
&
<
>
=
!=
==
Sample output:
Explanation / Answer
Answer:
Note: Modified the given Code as per user requirements.
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.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;
void printBinaryAndHex (unsigned int u)
{
unsigned int place;
printf(" ");
for(place=1<<31;place>0;place=place/2)
if(u&place)
printf("%c",ONE);
else
printf("%c",ZERO);
printf(" ");
printf("0x%08X",u);
}
void bitSlider ()
{
unsigned int u0;
unsigned int uAccumulator = ZERO;
for(int u0=0;u0<=31;u0++)
{
for(int u1=0;u1<=31;u1++)
{
unsigned int p=pow((float)2,u1);
unsigned int p1=pow((float)2,u0);
unsigned int p3=p|p1;
printBinaryAndHex(p3);
}
}
}
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);
numRead = sscanf(text,"%X",&u);
}
while (numRead < ONE);
return(u);
}
// 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=10;
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;
while ( (uChoice = getHexadecimalInt()) != ZERO )
{
// HINT:
// Have 2 loops.
// The outer loop will iterate about halfBitsOfWord times.
// The inner loop rolls by the amount given in the outer loop.
for(unsigned int k1=1;k1<=halfBitsOfWord;k1++)
{
printBinaryAndHex(uChoice);
unsigned int low=uChoice&BYTE_WITH_ALL_BITS_ON;
unsigned int high=uChoice>>16;
unsigned int rollLeft=high<<k1;
unsigned int rollRight=low>>k1;
uChoice=(rollLeft<<16)|rollRight;
}
}
}
int main()
{
bitSlider();
rollHighBitsLeft_rollLowBitsRight();
getch();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.