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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:03:15+00:00 2026-06-15T22:03:15+00:00

I saw a line of code like this: xxxxx = (uint16_t) -1; Why cast

  • 0

I saw a line of code like this:

xxxxx = (uint16_t) -1;

Why cast -1 into a unsigned int? What is this code to get?

Thanks!

  • 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-15T22:03:17+00:00Added an answer on June 15, 2026 at 10:03 pm

    Obviously the answer is within reach of your fingertips. Just plug that code in and step through and you will see the value that you get when you cast -1 to a 16 bit unsigned integer.

    The value turns out to be two’s complement of -1, which is: 0xFFFF hex, or 65535 decimal.

    As to the actual reason for using the code like that, it’s simply a short-cut. Or maybe it’s just to satisfy a type compatibility requirement.

    If you’re wondering how come -1 gets cast to 0xFFFF (65535) and not maybe 0 or 1, as one might expect, you have to understand that the reason for that is that the C language, although statically typed, is quite liberal when it comes to enforcing type restrictions. That means that it will happily cast – or interpret if you will – any memory location as whatever arbitrary type of data you tell it. This of course can have quite devastating consequences if used improperly but the trade-off is flexibility and a speed improvement due to the lack of strict sanity checks. This was very important a few decades ago when C was designed and it still is if you’re writing code for very tiny processors.

    That being said, if you think about a cast as simply saying: “disregard what you think you know about the data at this memory location, just tell me what it would mean if you read it as a <insert_your_type_here>“ and if you know that computers usually represent negative numbers as two’s complement (see above) then the answer should by now by pretty obvious: C is taking the value in memory and reading it back as an unsigned integer.

    As an ending note. I should point out that C is not the only language that will cast -1 to 0xFFFF but even more modern languages that are capable of stronger type checks will do the same, probably for compatibility and continuity reasons, as well as for the reason that it makes it possible to reverse the cast: 0xFFFF back to a signed 16 bit integer is -1.

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

Sidebar

Related Questions

I saw some code written by another developer that looks something like this: var
I was looking into this ITE8712 watchdog timer demo code when I saw this:
I saw a line of C that looked like this: !ErrorHasOccured() ??!??! HandleError(); It
I saw this code in Erlang: [X-$0 || X<-someFun()] In that line I found
I recently saw this page: PHP cli command line safe_mode restriction Where it is
I'm looking at some external code and saw a line of Ruby code that
Today i saw this bad code structure and since then i started to think
I am trying to use the set_intersection() function. My code looks like below set<int>
Short question, I saw in some old code where a ByteArrayInputStream was created like:
I am wondering if someone could explain this line of code of DB2 in

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.