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

  • Home
  • SEARCH
  • 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 8907231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:51:37+00:00 2026-06-15T02:51:37+00:00

I recently did a Java course (1 week crash course), and we covered some

  • 0

I recently did a Java course (1 week crash course), and we covered some binary mathematics.

This unary ~ operator (tilde I think it’s called?) was explained to us thus:

It inverts the bit pattern turning every “0” into a “1” and every “1” into a “0”.
e.g. There are 8 bits to a byte. If you have the following byte: 00000000 the inverted value would change to become 11111111.

The above explanation is clear and concise, and totally makes sense to me. Until, that is, I try to implement it.

Given this:

byte x = 3;
byte y = 5;
System.out.println(~x);
System.out.println(~y);

The output is:

-4  
-6

I’m very confused about how this happens.

If +3 in binary is 11, then the inversion of this would be 00, which clearly isn’t -3.

But as there are 8 bits in a byte, then shouldn’t the binary representation of +3 be written as 00000011?

Which would invert to become 11111100. Converted back to decimal value this would be 252.
If however you write the +3 as 011, then it does indeed convert to 100, which is +4, but then how do you know it’s a negative number?

How about if you try 0011, which converts to 1100, which if you use the first bit as a sign, then it does indeed become -4.

Ah – so at this point I thought I was getting somewhere.

But then I got to the second value of y = 5.

How do we write this? Using the same logic, +5 converts to binary 0101, which inverts to 1010.

And it’s around now that I’m horribly confused. This looks to represent either a signed value of -2, or an unsigned value of +10 decimal? Neither of which are the -6 I’m getting printed out.

Again, if I increase the length up to the 8 digits of a byte, +5 is 00000101, which inverted becomes 11111010. And I really can’t find a way to turn this into -6.

Does anyone out there understand this, as I have no idea what is happening here and the more numbers I print out the more confused I become.

Google doesn’t seem to come up with anything much on this – maybe it doesn’t like looking at little operator signs.. 🙁

  • 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-15T02:51:38+00:00Added an answer on June 15, 2026 at 2:51 am

    From wikipedia: In two’s complement notation, a non-negative number is represented by its ordinary binary representation; in this case, the most significant bit is 0. The two’s complement operation is the negation operation, so negative numbers are represented by the two’s complement of the absolute value.
    To get the two’s complement of a binary number, the bits are inverted, or “flipped”, by using the bitwise NOT operation; the value of 1 is then added to the resulting value, ignoring the overflow which occurs when taking the two’s complement of 0. http://en.wikipedia.org/wiki/Two%27s_complement

    So if you have 0101 which is +5 the inverse of that is 1010, which is -5.

    You don’t really read the 010 as a 5 though, but when you see the 1 at the beginning, you know that to get the number you have to invert the rest of the digits again to get the positive number which you want to negate. If that makes sense.

    It’s a bit of an alien concept if you have not worked with it before. It’s certainly not the way that decimal numbers work, but it is actually simple once you see what happening.

    A value of 8 decimal is written as 01010, which negates to 10101. The first digit (1) means it’s negative, and then you flip the rest back to get the numeric value: 1010.

    One thing to remember is that Two’s complement is not the same as ordinary old binary system counting. In normal binary the value of 10101 (which in Two’s complement is -8 as above) is of course 21. I guess this is where the confusion comes – how do you tell the difference by looking at them? You must know which representation has been used in order to decide what the value of the number actually is. There is also One’s complement which differs slightly.

    A good tutorial on binary maths, including One’s and Two’s complement is given here. http://www.math.grin.edu/~rebelsky/Courses/152/97F/Readings/student-binary

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

Sidebar

Related Questions

I recently did a git stash , then did some work on the branch
Recently I did a Java programming exercise successfully which was sent by a recruiting
Was asked this question recently and did not know the answer. From a high
So I am a Java and C# person recently doing some stuff C. I
So i am developing Java applications in Eclipse. Until now i did this on
I have recently developed some java applications that i want others to run on
I recently did some rough performance measuring on List<> vs [] for an array
Hi I have done Java and C# and recently did a module in C/C++
I recently did a Oracle to SQL Server 2008 R2 Migration. The PL/SQL stored
I recently did an online installation of SP1 for VS 2008, where installation of

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.