Thread: Changing leftmost digit of hexadecimal number.

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    4

    Changing leftmost digit of hexadecimal number.

    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;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > temp = digit << 7;
    Each hex digit is 4 bits, so perhaps you need to be thinking about things like

    mask = 0xF << (4 * digit);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    4
    Using masks? I'm not sure how to use those. Is there a way for me to change the leftmost digit, without that? I want to input 0xB and change the 5 in 0x5F8FDF91 to B. I'm just trying to make sense of these instructions specifically:
    "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."
    Thank you for all your help.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I think you need to look up the meaning of left and right.

    Code:
    clear = num1 & 0xFFFFFFF0; //This makes the rightmost digit able to be changed. That I can do.
    The above clears (or sets to zero) the right most nibble of the result that is stored in clear in this case. The rest of your comments implies you wish to do the left most nibble (4 bits are called a nibble).

    Tim S.
    Last edited by stahta01; 07-19-2012 at 01:02 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by thecityofjuly View Post
    Using masks? I'm not sure how to use those. Is there a way for me to change the leftmost digit, without that? I want to input 0xB and change the 5 in 0x5F8FDF91 to B. I'm just trying to make sense of these instructions specifically:
    "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."
    Thank you for all your help.
    I bolded the parts that make it clear that you need to use masks. The and and or statements will have to work with masks in this assignment. And actually you are already using a mask in your current code:
    Code:
        clear = num1 & 0xFFFFFFF0; //This makes the rightmost digit able to be changed. That I can do.
    The magic value 0xFFFFFFF0 is a mask to get rid of the rightmost digit. To do the same for the leftmost digit you will have to find which digit is the leftmost and compute your own mask from that.

    Edit: it's not clear from the instructions if you need to change the leftmost non-zero digit, or just the leftmost digit in a 32-bit value. If it's just the leftmost digit, whether it's 0 or not, then the mask is easy to compute. You just have to left-shift the new digit to the correct position.
    Last edited by christop; 07-19-2012 at 01:04 PM.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You can do away with the masking by using two shifts, in this instance.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    4
    Well, I am still learning, so it's not all the clear that to me that I must use masks and my book isn't a lot of help. My number is 0x5F8FDF91, if the user enters B, then the 5 in my hex number changes to B, not the 0x part. Thanks.

  8. #8
    Registered User
    Join Date
    Jul 2012
    Posts
    4
    Yeah, that's what I understood from the directions I was given, which tells me to "Clear the last hex digit using an and statement and put into temp", and from my understanding that's what
    Code:
     
    clear = num1 & 0xFFFFFFF0; 
    temp = clear;
    does. Since the rightmost is the last hex digit. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding a number digit by digit.
    By sdfx in forum C Programming
    Replies: 4
    Last Post: 05-10-2011, 05:54 PM
  2. Replies: 2
    Last Post: 10-31-2009, 06:49 PM
  3. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 23
    Last Post: 09-21-2007, 03:00 PM
  4. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 1
    Last Post: 09-14-2007, 03:28 AM
  5. outputs number digit by digit
    By Unregistered in forum C++ Programming
    Replies: 14
    Last Post: 05-17-2002, 04:43 PM

Tags for this Thread