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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:44:50+00:00 2026-05-18T06:44:50+00:00

When I compile the following code, my compiler complains that the following line is

  • 0

When I compile the following code, my compiler complains that the following line is always true. I think I may have a faulty understanding of the != operator…

if (pktNum != ~invPktNum) {
    return 1;
}

I’m trying to verify that invPktNum is indeed the inverse of pktNum. If not, exit immediately, otherwise proceed as normal.

I’ve checked and pktNum an unsigned char that is 0x01 and invPktNum is an unsigned char that is 0xFE at the time of comparison.

Can anybody enlighten me? Thanks in advance!

  • 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-18T06:44:51+00:00Added an answer on May 18, 2026 at 6:44 am

    In C, values in most expressions that are of a type narrower than int are promoted to a wider type before the calculation takes place. If int is wide enough to hold all the values of the narrower type, then it is promoted to int; otherwise it is promoted to unsigned int.

    In this case, int is wide enough to hold all the values of your unsigned char, so your values are promoted to int.

    pktNum (and thus, the promoted pktNum) can have a value between 0 and 255 inclusive. This is the value that will be used on the left hand side of the != operator.

    invPktNum can likewise have a value between 0 and 255 inclusive. This value will be promoted to int, and then bitwise-negated. The result of this bitwise negation will always be a negative number, since the sign bit will be negated. This is the value that will be used on the right hand side of the != operator.

    No negative number can ever be equal to the promoted pktNum, so the condition is always true.

    To perform the calculation that you actually want to, you need to mask out the lower eight bits after the negation:

    if (pktNum != (~invPktNum & 0xff)) {
        return 1;
    }
    

    Or alternatively, you can just negate the bits you’re interested in:

    if (pktNum != (invPktNum ^ 0xff)) {
        return 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to compile the following line of code from http://code.google.com/p/enhsim : enh::eout <<
I have the following code and it fails to compile template < typename T
Can the following code be expected to work in all environments that have an
Why is it that in the code below the compiler complains that PureAbstractBase is
I am trying to compile the following code: private String dataToString(){ Map data =
The following code doesn't compile with gcc, but does with Visual Studio: template <typename
The following code fails to compile stating A local variable named 'st' cannot be
The following code does not compile: public class GenericsTest { public static void main(String[]
The following Code does not compile Dim BasicGroups As String() = New String() {Node1,
This following code gets this compile error: " invalid types 'int[int]' for array subscript

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.