Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Negative Numbers in Bits

L Sol
Greenhorn
Posts: 3
How do you calculate the following number is equal to -6?
1111 1111 1111 1111 1111 1111 1111 1010
Thanks!

Richard Quist
Ranch Hand
Posts: 96
Originally posted by L Sol:
How do you calculate the following number is equal to -6?
1111 1111 1111 1111 1111 1111 1111 1010
Thanks!

Since negative numbers are represented in two's-complement form you need to
• Flip all the bits
• restore the negative sign

• original: 1111 1111 1111 1111 1111 1111 1111 1010
inverted: 0000 0000 0000 0000 0000 0000 0000 0101
0001
----
0110 (which is 6 in base 10)
Restore negative sign: -6

Serghei Jelauc
Ranch Hand
Posts: 128
Let say we have 1111 1101
Change all 1 and 0 vice versa: 0000 0010
0000 0010 = 2.
After it add 1: now have 3