Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 1347
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 30, 20222022-05-30T13:23:13+00:00 2022-05-30T13:23:13+00:00

linux – Bitwise operations in C yields unwanted 1’s or F’s

  • 0

[ad_1]

I have a problem with C’s bitwise operations and can’t “merge” two numbers together.
Suppose I have the number 0x0A (or any 2 hexadecimal digits) and I want to replace the left digit with 0xF for example, That is:
00001010 -> 11111010 in terms of bitwise.

What I tried to do is the following:

  1. We drop the left digit of the 2-digit hex number by left shifting (sizeof(int)*8-4) times, thus leaving the rightmost bits that we care about, and then right shifting (unsigned) so that we get only the right hex digit in the end. In the example above it won’t matter as it’ll yield ‘A’ which is the same as 0x0A.
  2. The second step is to “merge” the digit we got in step one and the shifted hex digit we want to use, and we’ll do that by using an OR gate.

Thus:

Hex number before and after step one: A (1010)
Desired hex digit after shifting: F0 (1111 0000)
OR gate:

00001010
OR 11110000
= 11111010 = 0xFA

However, for some reason, my C code does all of the above correctly, and just when applying OR it gives the result 0xFFFFFFFA (8-byte).

Is there something that I’m missing on how C works with bitwise?

Note that I use sizeof(int) because the code should be able to account for 4 or 8 byte int.

Thanks for help in advance!

int setHexDigit(char* digitPairPtr, int digitPos, char hexDigit)
{
    int return_value = 0;
    if( (hexDigit>='0' && hexDigit<='9') || (hexDigit>='A' && hexDigit <='F'))
    {
        if(digitPos==0)
        {   
            hexDigit = ((hexDigit<='9' && hexDigit>='0') ? (hexDigit-'0') : ( (hexDigit>='A' && hexDigit<='F') ? (hexDigit-('A'-10)) : (-1))); //THIS LINE IS NOT IMPORTANT 
            *digitPairPtr = ((unsigned)*digitPairPtr<<((sizeof(int))*8-4))>>((sizeof(int))*8-4); //Drop left digit.
            hexDigit = (hexDigit << 4);
            *digitPairPtr = *digitPairPtr | hexDigit;
            return_value=1;
        }
        else
        {
            //For later
        }
    }
    return return_value;
}

int main()
{
    char c = 0x0A;
    setHexDigit(&c,0,'F');
    printf("\n%X", c);
    return 0;
}

[ad_2]

  • 0 0 Answers
  • 13 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 5252 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1098 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1047 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.