惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
P
Proofpoint News Feed
The Last Watchdog
The Last Watchdog
S
Secure Thoughts
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
News | PayPal Newsroom
Application and Cybersecurity Blog
Application and Cybersecurity Blog
O
OpenAI News
S
Security @ Cisco Blogs
宝玉的分享
宝玉的分享
Hacker News: Ask HN
Hacker News: Ask HN
T
Troy Hunt's Blog
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
TaoSecurity Blog
TaoSecurity Blog
Help Net Security
Help Net Security
Latest news
Latest news
NISL@THU
NISL@THU
S
Security Affairs
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 聂微东
AI
AI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
小众软件
小众软件
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
AWS News Blog
AWS News Blog
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
I
InfoQ
Schneier on Security
Schneier on Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
The Exploit Database - CXSecurity.com
IT之家
IT之家
T
Threatpost
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
腾讯CDC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
N
News and Events Feed by Topic
L
LINUX DO - 最新话题
F
Full Disclosure
大猫的无限游戏
大猫的无限游戏
H
Heimdal Security Blog
S
SegmentFault 最新的问题

KIRUPA | Designers and Developers Unite

Understanding Merkle Trees Is a CompSci Degree Still Valuable in the Age of AI? The Model Context Protocol (MCP) Explained Vibe Coding + Expertise = Mega Win! 🏆 Animating our Grid Counting in Binary and Hexadecimal Pascal Pixel on Design, Development, and Solopreneurship! Do we really need to know how things work? 🧠 Drawing Sharp Lines on the Canvas The KIRUPA Tech Stack : It Bloom Filter: A Deep Dive Hash Functions Deep Dive Advanced Glitch Effect with Sound AI Killed the Content Creator...Star 🤩 Measuring the Distance Between Two Points by using the Pythagorean Theorem Detecting Browser Zoom Changes in JavaScript Creating a Fullscreen Grid Drawing a Perfect Grid on the Canvas Preserving the Pixel Art Look in Web Content Ensuring our Canvas Looks Good on Retina/High-DPI Screens Finding Prime Numbers Using a Sieve of Eratosthenes Two-Dimensional (2D) Arrays in JavaScript Two-Dimensional (2D) Arrays in JavaScript Animations: From Biology to JavaScript! 🦠 You’ll Always be Building & Designing Creating a Cluster Growth Animation: From Biology to JavaScript Timsort: A Lightning Fast Hybrid Sorting Algorithm Merge Sort: A Simple Step-by-Step Walkthrough 😀 - YouTube Bubble Sort: A Detailed Deep-Dive 🛁 Insertion Sort: A Deep Dive! 🍣 Selection Sort: A Step-by-Step Guide 💬 Radix Sort: A Complete Guide to Fast and Efficient Sorting! ⚡️ Career Growth Secrets Counting Sort : A Friendly (yet Detailed!) Deep Dive! 🎯 Bogosort: Sorting in the Slow Lane! 🐢 Pulling Off a Successful Redesign Creating Your Own Perfect Timing Radix Sort Making Counting Sort Work with Negative Values Diving Deep into Array Index Positions The Career Three Body Problem Counting Sort Work on Problems You Deeply Care About The Importance of Finding a Career Mentor Creating a Random Walk Simulation What is Product Strategy? Thinking about an 8K Resolution Future! 📺 Creating an Animated 3D Starfield Effect Meet the Default Sorting Algorithms! Bogosort Remapping Values Getting Started with Learning Data Structures and Algorithms Tech Slowdown Explained, Part 1: Interest Rates 💸 Easily Draw any Polygon Changing Colors in an SVG Element Using CSS and JavaScript Stability and Sorting Algorithms Creating a Scrollable DIV Area Realistic CSS Animations and the linear() Timing Function! 🍱 Visualizing Recursion with the Sierpinski Triangle Fast Sorting with Quicksort The Monty Hall Problem Stacks in JavaScript Depth-First Search (DFS) and Breadth-First Search (BFS) Introduction to the Graph Data Structure Big-O Notation and Complexity Analysis Introduction to Data Structures Arrays: A Data Structure Deep Dive Hashtables: A Deep Dive into Efficient Data Storage and Retrieval Trie (aka Prefix Tree) Embracing Generative AI with Open Arms! 🧸 Impact of AI on UI/UX Design with Chloe Barreau 🎨 Heap Data Structure Binary Search Trees Binary Tree Traversal Alphabetically Sort Names in an Array Overlapping Elements on Top of Each Other Developer Relations and Beyond with Jamie Barton! 🚀 A Trip Down Memory Lane 💾 Binary Trees Linked List The Present and Future of AI Tools with Ray (aka devbyrayray) "Guess the Number" and Binary Searching! 🔍 Switching Web Hosts in 2023 😱 SVG: Converting Shape to Path The Versatility of SVGs 🌀 Spinning Circular Text Introduction to Trees Faster Searching with Binary Search Search Algorithms and Linear Search Fibonacci and Going Beyond Recursion Guess the Number Game
How to Count in Negabinary (Base (-2)) — A Visual Guide
Kirupa Chinnathambi · 2025-06-17 · via KIRUPA | Designers and Developers Unite

Master negabinary (base (–2)) with visual examples! Learn how to count, convert, and do math without sign bits in this fun guide to negative-base numbers.

In our earlier look at non-decimal number systems that our computers love, we learned how to work with binary (base-2) and hexadecimal (base-16) values:

Today, we are going to go one step further. We are going to take everything we learned and look at how to work with negabinary or base-(-2) numbers. That is right! We are going to learn all about base negative two.

Onwards!

Why Learn Negabinary (base-(-2))?

Unlike binary and hexadecimal values, which can be found all over the place with just a little bit of digging in the right spots, negabinary values are more rare. Unless you are doing something very specialized, you’ll likely never run into it. The way I ran into it was purely by accident. I was reading this interview with the legendary computer programmer Charles Simonyi.

In this interview, he mentioned that one of his favorite interview questions was the following involving negabinary values:

With all of this said, learning about negabinary numbers isn’t just for trivial purposes. There are some interesting quirks when dealing with negative base values that may come in handy in unexpected ways. In the following sections, let’s dig deeper into how negabinary numbers work and wrap things up by also answering Charles’s question.

Understanding Negabinary Numbers

Negabinary works similarly to binary in that the output numbers we’ll be dealing with are just 0’s and 1’s. There is a crucial twist, though. Each position in the negabinary base-(-2) system represents a power of -2 rather than 2. This means the place values alternate between positive and negative powers of 2.

Going from Decimal to Negabinary

To convert a number from decimal to negabinary), we’ll use the general approach we’ve seen a few times already when looking at binary and hexadecimal values. We repeatedly divide the decimal number by the base value (-2 in our case) and record the remainder that is in the range of the expected output (either 0 or 1). We keep repeating this until the quotient from all our dividing reaches 0. Once that happens, we group all of the remainders together from the bottom to the top. This grouping of remainders is our converted output.

With negabinary, there is a twist that revolves around our specific handling of negative remainders. This last detail is important, and we’ll cover that when we run into it. To help bring all of this to life, let’s start with an example. What we want to do is represent the number 42 in negabinary.

First, we start off by dividing 42 / -2, and this results in a value of -21 with a remainder of 0:

42 / -2 = -21 remainder 0  

This all seems straightforward. The next calculation will be to divide -21 by -2. The result will be 10 with a remainder of -1:

42 / -2 = -21 remainder 0  
-21 / -2 = 10 remainder -1 // Wait...is this allowed?

Now, here is a problem. Valid negabinary values can only be 0 or 1. A remainder value of -1 isn’t valid, so what do we do? We adjust the quotient and the remainder while keeping the relationship between the values valid:

More specifically, we adjust by adding:

  • 1 to the quotient
  • 2 to the remainder

With these changes, our quotient is now an 11, and our remainder is increased by 2 to now become a positive 1.

Taking a step back, what we did isn’t arbitrary. We “borrowed” 1 from the quotient and “paid back” with 2 in the remainder. We essentially rewrote the same number in a way that conforms to the negabinary's digit rules. We didn't change the math — we just expressed it in a format that fits base –2.

To validate that there was no funny business, this algebraic formula explains the relationship between the divisor, dividend, quotient, and remainder:

If a / b = c remainder d, then bc + d = a

Using this approach, when we plug in the appropriate post-adjusted values...

-21 / -2 = 11 remainder 1, then -2 * 11 + 1 = -21

...we can see how everything squares up nicely with a result of -21.

Continuing this approach as we keep dividing the quotient by -2 and course correcting to ensure we only get valid 0 or 1 remainders, the full series of steps until our quotient hits 0 is:

42 / -2 = -21 remainder 0  
-21 / -2 = 11 remainder 1  
11 / -2 = -5 remainder 1  
-5 / -2 = 3 remainder 1  
3 / -2 = -1 remainder 1  
-1 / -2 = 1 remainder 1  
1 / -2 = 0 remainder 1

Collecting all of the remainders up from the bottom, we get 1111110. This 1111110 value is the negabinary representation of 42.

Going from Negabinary to Decimal

It’s time for us to go the other way around and look at the steps it takes to go from a negabinary value to a decimal value. This approach is FAR less dramatic. The main work is for us to map our negabinary values to their equivalent powers of -2:

If we do this mapping for the negabinary value of 1111110, we will start at the right-most value and start incrementing the power of -2 as we go left and have no more values to map at the end. Starting from 0, we increment the power by 1. For this example, our mapping will look as follows:

What we just did was the hardest part. Now, all that remains is to expand out the calculations and see what the final value is. This will start off as follows:

1 x (−2)6 + 1 x (−2)5 + 1 x (−2)4 + 1 x (−2)3 + ⏎
1 x (−2)2 +1 x (−2)1 + 0 x (−2)0

We can now simplify the results further and get the final answer:

64 − 32 + 16 − 8 + 4 − 2 + 0 = 42

The final result of turning the negabinary value of 1111110 into a decimal value is 42. We saw the reverse of this earlier, where we looked at how to turn 42 into negabinary, so that this all meshes up in both directions should not at all be surprising...even though it does feel like it sometimes!

Charles Simonyi’s Question

Getting back to Charles Simonyi and his negabinary-related interview question, below is an expanded snippet that talks about a few historical things about his time at Microsoft along with his negabinary-related interview question:

So yeah, I mean those days of Microsoft were incredible. We put together, we spent a lot of time hiring. I remember, for example, Bill was in the middle of everything, and we were hiring people so we were interviewing side by side at Berkeley, for example, and Princeton. And it's funny that sometimes Bill was overhearing my questions and vice versa, and at that time I was very keen on this question of base negative two. In my earlier life I saw a machine that was operating in base negative two that was a Polish serial computer. Maybe the Museum could have some record of it, but it turns out that in base negative two, there's no issue about how to represent negative numbers, so you don't have to decide as it were, you know, two's complement, one's complement, sign magnitude and all of those things that were really occupying a great deal of bandwidth during the 50s. So these guys, being mathematicians, they said well, there's one way out of it, we use base negative two, where there's no sign and negative number representation is completely well defined. But I made an exam question out of it and just said, "How do you tell, I'll give you a binary number, it's in base negative two, how do you tell whether the number is positive or negative?" And of course the answer is that you count the position of the leading bit and if it's even then it's negative, and if it's odd it's positive or the other way around, but so because the interview of positions they are worth one, minus two, four, minus eight and so on, so forth. And you know, it's something that can be solved in two or three minutes and it really distinguishes people who really can follow up on some rules in a conceptual way and those that are completely frightened by something that they haven't heard before.

In a negabinary system (base -2), you don’t need a sign bit to tell you whether a number is positive or negative. The sign of the number is built into the pattern of the digits because the place values alternate between positive and negative powers of 2.

Remember, each digit in a negabinary number represents a power of -2:

If the position of the leading bit is on an even position (0, 2, 4, 6, etc.), the number is going to be positive:

If the position of the leading bit is on an odd position (1, 3, 5, 7, etc.) then the number is going to be negative:

By knowing this little detail, negabinary allows us to both quickly identify whether an number is positive or negative as well as help us represent negative and positive values very elegantly. This characteristic makes negabinary a very viable choice in low-level systems programming, where you are most likely to encounter it.

Conclusion

So there you have it — negabinary, the quirky number system that flips everything (literally) on its head. What starts out sounding like a math puzzle — “let’s use negative powers of two!” — turns out to be a surprisingly elegant way to handle both positive and negative numbers without needing sign bits or complicated hacks like two’s complement. Sure, it’s not what your calculator uses, and you and I probably won’t see it pop up in your next coding interview unless you are talking to Charles Simonyi, but it’s a good mental exercise to think about how to deal with negative base values when converting between number systems.

Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!