• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

2's complement without writing 1's complement

 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the method which I learnt during my electronic days at college to get 2's complement without writing one's complement.
Let me start with example which makes most of us to understand things more better.


Let us represent -23 in binary form

+23 can be represented as follows
00000000000000000000000000010111
<---
start from right to left , keep on writing the same digit till you find a digit which is 1 (in our case it is the first digit itself ) after that simply invert the digits.
i.e
00000000000000000000000000010111
^1 ( ^ = start inverting here )
so -23 would look like
11111111111111111111111111101001 = -23
^

Similary based on the above rule we can find -20 as follows
Start with +20
00000000000000000000000000010100 = 20

start from right to left
00000000000000000000000000010100
^100 ( ^ = start inverting here )
Write the same digits till you encounter the first 1 and then flip the remaining ones
so -20 would look like
11111111111111111111111111101100



Hope this helps everybody. Experts please give feedback.
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Formatting was messed up when I posted the previous explanation
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice tip Jay! Funny, in all my years of twiddling bits, I have never come across that one.

(I think I negated my first binary number in 1969 ! )
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic