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 a 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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8646905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:55:58+00:00 2026-06-12T12:55:58+00:00

unsigned char xor4(unsigned char c1, unsigned char c2){ int i = 0; while(i <

  • 0
unsigned char xor4(unsigned char c1, unsigned char c2){
    int i = 0;
    while(i < 8){
        if((getBit(c1, i) ^ getBit(c2, i)))
             setBit(c1,i);
        else clearBit(c1, i);
        i+=2;
    }

    return c1;
}

The above code is supposed to be a very simple function to set every other bit in a character based on the result of an xor with every other bit in a second character. For some reason, this simply doesn’t work. The program seems to just ignore my while loop and return the original function.

Oh and here are my getBit, setBit and clearBit functions.

unsigned char getBit(unsigned char c, int n){
  return (c & 1<<n) >> n;
}

unsigned char clearBit(unsigned char c, int n){
  c = c & (~(1<<n));
}

unsigned char setBit(unsigned char c, int n){
  `c = c | (1<<n);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:55:59+00:00Added an answer on June 12, 2026 at 12:55 pm

    You didn’t post your setbit and clearbit functions, but whatever they do, that can’t change c1 in the calling function. You need to either pass the address of c1 or return the new value of c1. Or you can just use C bitwise operators. And your entire operation is just equivalent to c1 ^= c2 (assuming that a char has 8 bits).

    Edit: Given your edit, just assign the return values of your functions to c1 … and fixe your setbit and clear bit functions … they don’t return values, and if you were using proper warning settings in your compiler it would tell you that. I’ve made some stylistic changes:

    /* your getbit works but is needlessly complex */
    unsigned char getBit(unsigned char c, int n){
        return (c >> n) & 1; 
    
    unsigned char clearBit(unsigned char c, int n){
        return c & ~(1 << n);
    }
    
    unsigned char setBit(unsigned char c, int n){
        return c | (1 << n);
    } 
    
    unsigned char xor4(unsigned char c1, unsigned char c2){
        for( int i = 0; i < 8; i += 2 )
            c1 = ( getBit(c1, i) ^ getBit(c2, i)
                   ? setBit(c1, i);
                   : clearBit(c1, i) );
    
        return c1;
    }
    

    Here’s a faster solution:

    unsigned char xor4(unsigned char c1, unsigned char c2){
        return (c1 & 0xAA) | ((c1 ^ c2) & 0x55);
    }
    

    Even faster:

    unsigned char xor4(unsigned char c1, unsigned char c2){
        return (c2 & 0x55) ^ c1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code int ParseData(unsigned char *packet, int len) { struct ethhdr
If I have these code: unsigned char **keys; int *num_keys; int num_images = (int)
unsigned char* Data::getAddress(unsigned char* address) { strcpy((char*)address, (char*)this->_address); return (unsigned char*)address; } int main()
My code below gave a different length for the unsigned char pointer I expect.
I came across the below lines in code unsigned char A = 0xB9; unsigned
Suppose this code: unsigned char list[3] = { 1, 2, 3 }; struct _struct{
So far I have code to read an unsigned char from ifstream: ifstream in;
i have written this structure: struct bmpheader { unsigned char magic[2]; unsigned int fsize;
I need to store large amounts of unsigned char s and/or int s (potentially
error LNK2019: unresolved external symbol char * __cdecl BytesToString(unsigned char const *,unsigned int) (?BytesToString@@YAPADPBEI@Z)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.