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 7615355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:37:44+00:00 2026-05-31T02:37:44+00:00

This may be a stupid question but I can’t seem to figure this out.

  • 0

This may be a stupid question but I can’t seem to figure this out.

I’ve written a program the takes a char as input, outputs the char and its hexadecimal value then checks for even parity and sets the parity bit to 1 and again outputs the “new” char and it’s hex value. However using printf and %c doesn’t seem to be the way to go and I don’t understand why or how to fix it, so I would very much appreciate if someone could explain why not and what I should be doing instead. Oh and feel free to comment on the code I am learning after all so criticism is always good 🙂

int checkParity(unsigned char myChar); //returns 0 if even 1 if odd

int main()  {
  int i;
  unsigned char input;

  printf("Enter char: ");
  scanf("%c", &input);

  /* print unchanged char and hex with */
  printf("c7: %c ", input);
  printf("hex: %x \n", input);

  /*if parity is odd*/
  if(checkParity(input)){
    /*change to even*/
    input = (input|0x80);
  }

  /* print char and hex even parity */
  printf("c8: %c ", input);
  printf("hex: %x \n", input);  

  return 0;
}

int checkParity(unsigned char myChar){
  int i;
  int parity = 0;

  while(myChar){    //while myChar != 0
    parity += (myChar&1);   //add result of myChar AND 1 to parity
    myChar = myChar>>1;     //shift bits left by 1
  }
  //printf("parity equals: %d\n", parity);
  if(parity%2){ // if odd parity
    return 1;
  }
  else { //even parity
    return 0;
  }
}
  • 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-05-31T02:37:44+00:00Added an answer on May 31, 2026 at 2:37 am

    What are parity bits?

    Parity bits are a simple form of error-checking that can be used, for example, when you transmit data from one machine to another. If both machines agree on, say, even parity, then the receiver will know that any incoming characters that don’t have even parity were received in error. However, parity of any type can only identify an odd number of erroneous bits: two errors in a single character will cancel each other, and the resulting parity will be correct. Moreover, the receiver cannot determine which bit (or bits) are incorrect; parity provides error detection, but not error correction.

    How should received data be processed?

    The receiver should calculate and validate the parity of each incoming character. If it detects a character with invalid parity, it should indicate the error somehow. In the best case, it may be able to ask the transmitter to re-transmit the character.

    Once the character is validated, the receiver must strip the parity bit before passing the character on for further processing. This is because, since the parity bit is used for error detection, it is “lost” from the data payload. Thus, enabling parity will reduce the number of available data values by half. For example, 8 bits can have one of 256 possible values (0 – 255). If one bit is used for parity, only 7 bits remain to encode the data, leaving only 128 valid values.


    Since you asked for comments/criticism, here’s a revised, commented version of your code:

    #include <stdio.h>
    
    // Consider using a boolean data type, as the function returns
    // "true" or "false" rather than an arbitrary integer.
    int isOddParity(unsigned char myChar);
    
    
    int main(void) {
      unsigned char input;
    
      printf("Enter char: ");
      scanf("%c", &input);
    
      // Force even parity by setting MSB if the parity is odd.
      unsigned char even = isOddParity(input) ? input | 0x80 : input;
    
      // Print the original char, original hex, and even-parity hex
      // Print hex values as 0xHH, zero-padding if necessary.  This program
      // will probably never print hex values less than 0x10, but zero-padding
      // is good practice.
      printf("orig char: %c\n", input);
      printf("orig  hex: 0x%02x\n", input);
      printf("even  hex: 0x%02x\n", even);
      return 0;
    }
    
    // Calculate the parity of myChar.
    // Returns 1 if odd, 0 if even.
    int isOddParity(unsigned char myChar) {
      int parity = 0;
    
      // Clear the parity bit from myChar, then calculate the parity of
      // the remaining bits, starting with the rightmost, by toggling
      // the parity for each '1' bit.
      for (myChar &= ~0x80;  myChar != 0;  myChar >>= 1) {
        parity ^= (myChar & 1);   // Toggle parity on each '1' bit.
      }
      return parity;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may sound like a stupid question but I can't seem to find an
This may seem a stupid question but I can't find the anwer... This is
This question may sound a bit stupid, but I couldn't figure out, how to
This may seem like a stupid question, but i can't quite remember how to
This may be a really stupid question but I can't seem to find how
This may sounds like a stupid question but can't find anything on google, probably
This may be a stupid question but I can't help asking, just to verify.
This may sound like a stupid question, but can DI be used everywhere where
this may seem like a stupid question, but it is stumping me nontheless. I'm
This question may seem like a novice, and perhaps 'stupid' question but please bear

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.