I am trying to write a function that changes the first (leftmost) hexadecimal digit in a 32-bit unsigned integer using bitwise operators. The function needs to have two parameters: the first is the integer to be manipulated, the second the replacement digit. I can change the rightmost digit, but the leftmost digit ... This is what I have so far:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
uint32_t changeLeftmost (uint32_t num1, int digit);
int main (void) {
uint32_t num1 =0x5F8FDF91 ;
int digit;
int changedHex;
printf ("Here is the unchanged hex digit: %#06X\n", num1);
printf ("Enter the digit that will replace the leftmost digit:\n");
scanf ("%d", &digit);
changedHex = changeLeftmost (num1, digit);
printf("Changed leftmost digit:\n");
printf ("%#06X\n", changedHex);
system ("pause");
return 0;
}
uint32_t changeLeftmost (uint32_t num1, int digit){
/*
Your function should accept two parameters of uint32_t.
Inside the function create three variables of uint32, temp, result, and clear
Clear the last hex digit using an and statement and put into temp
Bit shift the replacement digit into the right place
Combine the two numbers together with an or statement
Return result to main.
*/
uint32_t temp;
uint32_t result;
uint32_t clear;
//This is where I am trying to shift and replace the digit.
clear = num1 & 0xFFFFFFF0; //This makes the rightmost digit able to be changed. That I can do.
temp = clear;
temp = digit << 7;
result = temp | num1;
return result;
}