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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:12:59+00:00 2026-06-01T17:12:59+00:00

In this little program: #include <unistd.h> #include <stdint.h> #include <stdio.h> int main() { uint8_t

  • 0

In this little program:

#include <unistd.h>
#include <stdint.h>
#include <stdio.h>

int main() {
    uint8_t a = 0;
    uint8_t b = 255;

    if (a == (b + 1)) {
        printf("Equal\n");
    } else {
        printf("Not equal\n");
    }

    if (a == ((b + 1) & 0xFF)) {
        printf("Equal\n");
    } else {
        printf("Not equal\n");
    }       
}

I get:

Not Equal
Equal

Why doesn’t the comparison work unless I forcibly take the last 8 bits? I’m guessing I’m missing some nuance of unsigned arithmetic here…

I’m using gcc 4.4.5 if that makes a difference.

  • 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-01T17:13:00+00:00Added an answer on June 1, 2026 at 5:13 pm

    Because of integer promotions, both operands of the == and + operator are promoted to int.

    The expression:

    a == (b + 1)
    

    is then equivalent to:

    0 == 256
    

    which is false.

    The expression: a == (uint8_t) (b + 1) would give you the result you expect (true). Another solution is to use & 0xFF like in your second if statement,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this little program: #include <stdio.h> int main() { char c = 0xFF; printf(%d\n,
I wrote this small C program: #include <stdio.h> #include <stdlib.h> int main() { FILE
#include <stdio.h> int main (){ int x=10,*a=&x; int *b=(int *)&a; printf(%d %d %d %d
i wrote the following program #include <stdio.h> main() { int i = 2; float
So here I got this little program which everyone obviously understands. include <iostream> include
I'm making this little program because I was bored and I ran across this
I have this little NetStream movie (which I'm not allowed to show, sorry) set
I wrote this little program in c++ to in order check CPU load scenarios.
I'm trying to make a little program with Qt. I have a main.cpp with
I am beginnner to programming. I wrote this little program to reverse a string.

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.