Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Sandbox

Post History

71%
+3 −0
Sandbox Add two negabinary integers [FINALIZED]

posted 3y ago by Moshi‭  ·  edited 11mo ago by trichoplax‭

#6: Post edited by user avatar trichoplax‭ · 2023-06-19T10:26:03Z (11 months ago)
Add finalized tag now that the sandbox can be filtered to exclude tags
Add two negabinary integers [FINALIZED]
## About Negabinary

Negabinary means base negative two (-2). That is, the $n$th place value is determined by $(-2)^n$.

Negabinary numbers can be evaluated just like any other base system. For example, we can parse the numbers 101, 110, and 1010 as follows:

| -8 | 4 | -2 | 1 | value |
|:--:|:-:|:--:|:-:|:-----:|
|    | 1 | 0  | 1 |   5   |
|    | 1 | 1  | 0 |   2   |
| 1  | 0 | 1  | 0 |  -10  |


## Challenge

Given two negabinary integers, output [the sum of them in negabinary](https://en.wikipedia.org/wiki/Negative_base#Arithmetic_operations).

You may take inputs in any format that makes sense. However, the output of the function/program must be in the same format as the input

The following ungolfed example takes in two 0-indexed arrays, with the nth entry being the nth place of each number.

```javascript
function addNegabinary(first, second) {
    let carry = 0;
    let result = [];

    let place = 0;
    while(place < first.length || place < second.length || carry) {
        let num = (first[place] || 0) + (second[place] || 0) + carry;
        let bit;
        switch (num) {
            case -1: bit = 1; carry = 1; break;
            case 0: bit = 0; carry = 0; break;
            case 1: bit = 1; carry = 0; break;
            case 2: bit = 0; carry = -1; break;
            case 3: bit = 1; carry = -1; break;
        }
        result.push(bit);
        place++;
    }
    return result;
}
```

[Try it online!](https://tio.run/##fVPLboMwELzzFXsEhUR2e6vbX@gPIB8cYwKtYyLbNIqafHvqRwCnpUEIrWdnZ1fr4YN9McN1d7Br1dfiem0GxW3XK2B1/S52bNsppk9502ljSzCC96ou4DsD90hhgTOtT/AGiEyQFmaQ1mEVJdmEHiTjYiYe206KPIKvEOQ3UqidbeF8hhGP/ZJEaDf2H6XVsHfCccYqlFLPRQWsII8Sv@GgQ@5Utp2dAXPsLG8hd9JpN/9wZgSs8YsvcG0xmXbgwq0W7JP85aORjkiysn/pS@oP6E8L6usH0zwvyC/wL1MUr3RzGEybu8pi5oS9rlYRiAVa2EGrWw3JLld3AcaCFcYa74qsqjAtoUKUlmOMkxiVgN0bEX9ICNMxcNBEux3SL3iej9IEDgmaeWc2vYbcTwV9E6dLjR3c5Mb1CTfqbO9oqDGD6YLxH/07ke5X0kuxkf3uPl/eVAq/uB8 "JavaScript (Node.js) – Try It Online")

## Some test cases

```
1 + 0 = 1
1 + 1 = 110        // 1 + 1 = 2
1 + 110 = 111      // 1 + 2 = 3
10 + 1 = 11        // -2 + 1 = -1
10 + 1011 = 110101 // -2 + -9 = -11

1010101 + 1110100 = 110011001
```
#5: Post edited by user avatar Moshi‭ · 2021-07-07T22:18:15Z (almost 3 years ago)
  • Add two negabinary integers
  • Add two negabinary integers [FINALIZED]
#4: Post edited by user avatar Moshi‭ · 2021-06-30T00:04:35Z (almost 3 years ago)
Added a short explanation of negabinary
  • ## Challenge
  • Given two negabinary integers (base -2), output [the sum of them in negabinary](https://en.wikipedia.org/wiki/Negative_base#Arithmetic_operations).
  • You may take inputs in any format that makes sense. However, the output of the function/program must be in the same format as the input
  • The following ungolfed example takes in two 0-indexed arrays, with the nth entry being the nth place of each number.
  • ```javascript
  • function addNegabinary(first, second) {
  • let carry = 0;
  • let result = [];
  • let place = 0;
  • while(place < first.length || place < second.length || carry) {
  • let num = (first[place] || 0) + (second[place] || 0) + carry;
  • let bit;
  • switch (num) {
  • case -1: bit = 1; carry = 1; break;
  • case 0: bit = 0; carry = 0; break;
  • case 1: bit = 1; carry = 0; break;
  • case 2: bit = 0; carry = -1; break;
  • case 3: bit = 1; carry = -1; break;
  • }
  • result.push(bit);
  • place++;
  • }
  • return result;
  • }
  • ```
  • [Try it online!](https://tio.run/##fVPLboMwELzzFXsEhUR2e6vbX@gPIB8cYwKtYyLbNIqafHvqRwCnpUEIrWdnZ1fr4YN9McN1d7Br1dfiem0GxW3XK2B1/S52bNsppk9502ljSzCC96ou4DsD90hhgTOtT/AGiEyQFmaQ1mEVJdmEHiTjYiYe206KPIKvEOQ3UqidbeF8hhGP/ZJEaDf2H6XVsHfCccYqlFLPRQWsII8Sv@GgQ@5Utp2dAXPsLG8hd9JpN/9wZgSs8YsvcG0xmXbgwq0W7JP85aORjkiysn/pS@oP6E8L6usH0zwvyC/wL1MUr3RzGEybu8pi5oS9rlYRiAVa2EGrWw3JLld3AcaCFcYa74qsqjAtoUKUlmOMkxiVgN0bEX9ICNMxcNBEux3SL3iej9IEDgmaeWc2vYbcTwV9E6dLjR3c5Mb1CTfqbO9oqDGD6YLxH/07ke5X0kuxkf3uPl/eVAq/uB8 "JavaScript (Node.js) – Try It Online")
  • ## Some test cases
  • ```
  • 1 + 0 = 1
  • 1 + 1 = 110 // 1 + 1 = 2
  • 1 + 110 = 111 // 1 + 2 = 3
  • 10 + 1 = 11 // -2 + 1 = -1
  • 10 + 1011 = 110101 // -2 + -9 = -11
  • 1010101 + 1110100 = 110011001
  • ```
  • ## About Negabinary
  • Negabinary means base negative two (-2). That is, the $n$th place value is determined by $(-2)^n$.
  • Negabinary numbers can be evaluated just like any other base system. For example, we can parse the numbers 101, 110, and 1010 as follows:
  • | -8 | 4 | -2 | 1 | value |
  • |:--:|:-:|:--:|:-:|:-----:|
  • | | 1 | 0 | 1 | 5 |
  • | | 1 | 1 | 0 | 2 |
  • | 1 | 0 | 1 | 0 | -10 |
  • ## Challenge
  • Given two negabinary integers, output [the sum of them in negabinary](https://en.wikipedia.org/wiki/Negative_base#Arithmetic_operations).
  • You may take inputs in any format that makes sense. However, the output of the function/program must be in the same format as the input
  • The following ungolfed example takes in two 0-indexed arrays, with the nth entry being the nth place of each number.
  • ```javascript
  • function addNegabinary(first, second) {
  • let carry = 0;
  • let result = [];
  • let place = 0;
  • while(place < first.length || place < second.length || carry) {
  • let num = (first[place] || 0) + (second[place] || 0) + carry;
  • let bit;
  • switch (num) {
  • case -1: bit = 1; carry = 1; break;
  • case 0: bit = 0; carry = 0; break;
  • case 1: bit = 1; carry = 0; break;
  • case 2: bit = 0; carry = -1; break;
  • case 3: bit = 1; carry = -1; break;
  • }
  • result.push(bit);
  • place++;
  • }
  • return result;
  • }
  • ```
  • [Try it online!](https://tio.run/##fVPLboMwELzzFXsEhUR2e6vbX@gPIB8cYwKtYyLbNIqafHvqRwCnpUEIrWdnZ1fr4YN9McN1d7Br1dfiem0GxW3XK2B1/S52bNsppk9502ljSzCC96ou4DsD90hhgTOtT/AGiEyQFmaQ1mEVJdmEHiTjYiYe206KPIKvEOQ3UqidbeF8hhGP/ZJEaDf2H6XVsHfCccYqlFLPRQWsII8Sv@GgQ@5Utp2dAXPsLG8hd9JpN/9wZgSs8YsvcG0xmXbgwq0W7JP85aORjkiysn/pS@oP6E8L6usH0zwvyC/wL1MUr3RzGEybu8pi5oS9rlYRiAVa2EGrWw3JLld3AcaCFcYa74qsqjAtoUKUlmOMkxiVgN0bEX9ICNMxcNBEux3SL3iej9IEDgmaeWc2vYbcTwV9E6dLjR3c5Mb1CTfqbO9oqDGD6YLxH/07ke5X0kuxkf3uPl/eVAq/uB8 "JavaScript (Node.js) – Try It Online")
  • ## Some test cases
  • ```
  • 1 + 0 = 1
  • 1 + 1 = 110 // 1 + 1 = 2
  • 1 + 110 = 111 // 1 + 2 = 3
  • 10 + 1 = 11 // -2 + 1 = -1
  • 10 + 1011 = 110101 // -2 + -9 = -11
  • 1010101 + 1110100 = 110011001
  • ```
#3: Post edited by user avatar Moshi‭ · 2021-06-29T23:18:58Z (almost 3 years ago)
  • ## Challenge
  • Given two negabinary integers (base -2), output [the sum of them in negabinary](https://en.wikipedia.org/wiki/Negative_base#Arithmetic_operations).
  • You may take inputs in any format that makes sense. However, the output of the function/program must be in the same format as the input
  • The following ungolfed example takes in two 0-indexed arrays, with the nth entry being the nth place of each number.
  • ```javascript
  • function addNegabinary(first, second) {
  • let carry = 0;
  • let result = [];
  • let place = 0;
  • while(place < first.length || place < second.length || carry) {
  • let num = (first[place] || 0) + (second[place] || 0) + carry;
  • let bit;
  • switch (num) {
  • case -1: bit = 1; carry = 1; break;
  • case 0: bit = 0; carry = 0; break;
  • case 1: bit = 1; carry = 0; break;
  • case 2: bit = 0; carry = -1; break;
  • case 3: bit = 1; carry = -1; break;
  • }
  • result.push(bit);
  • place++;
  • }
  • return result;
  • }
  • }
  • ```
  • [Try it online!](https://tio.run/##fVPLboMwELzzFXsEhUR2e6vbX@gPIB8cYwKtYyLbNIqafHvqRwCnpUEIrWdnZ1fr4YN9McN1d7Br1dfiem0GxW3XK2B1/S52bNsppk9502ljSzCC96ou4DsD90hhgTOtT/AGiEyQFmaQ1mEVJdmEHiTjYiYe206KPIKvEOQ3UqidbeF8hhGP/ZJEaDf2H6XVsHfCccYqlFLPRQWsII8Sv@GgQ@5Utp2dAXPsLG8hd9JpN/9wZgSs8YsvcG0xmXbgwq0W7JP85aORjkiysn/pS@oP6E8L6usH0zwvyC/wL1MUr3RzGEybu8pi5oS9rlYRiAVa2EGrWw3JLld3AcaCFcYa74qsqjAtoUKUlmOMkxiVgN0bEX9ICNMxcNBEux3SL3iej9IEDgmaeWc2vYbcTwV9E6dLjR3c5Mb1CTfqbO9oqDGD6YLxH/07ke5X0kuxkf3uPl/eVAq/uB8 "JavaScript (Node.js) – Try It Online")
  • ## Some test cases
  • ```
  • 1 + 0 = 1
  • 1 + 1 = 110 // 1 + 1 = 2
  • 1 + 110 = 111 // 1 + 2 = 3
  • 10 + 1 = 11 // -2 + 1 = -1
  • 10 + 1011 = 110101 // -2 + -9 = -11
  • 1010101 + 1110100 = 110011001
  • ```
  • ## Challenge
  • Given two negabinary integers (base -2), output [the sum of them in negabinary](https://en.wikipedia.org/wiki/Negative_base#Arithmetic_operations).
  • You may take inputs in any format that makes sense. However, the output of the function/program must be in the same format as the input
  • The following ungolfed example takes in two 0-indexed arrays, with the nth entry being the nth place of each number.
  • ```javascript
  • function addNegabinary(first, second) {
  • let carry = 0;
  • let result = [];
  • let place = 0;
  • while(place < first.length || place < second.length || carry) {
  • let num = (first[place] || 0) + (second[place] || 0) + carry;
  • let bit;
  • switch (num) {
  • case -1: bit = 1; carry = 1; break;
  • case 0: bit = 0; carry = 0; break;
  • case 1: bit = 1; carry = 0; break;
  • case 2: bit = 0; carry = -1; break;
  • case 3: bit = 1; carry = -1; break;
  • }
  • result.push(bit);
  • place++;
  • }
  • return result;
  • }
  • ```
  • [Try it online!](https://tio.run/##fVPLboMwELzzFXsEhUR2e6vbX@gPIB8cYwKtYyLbNIqafHvqRwCnpUEIrWdnZ1fr4YN9McN1d7Br1dfiem0GxW3XK2B1/S52bNsppk9502ljSzCC96ou4DsD90hhgTOtT/AGiEyQFmaQ1mEVJdmEHiTjYiYe206KPIKvEOQ3UqidbeF8hhGP/ZJEaDf2H6XVsHfCccYqlFLPRQWsII8Sv@GgQ@5Utp2dAXPsLG8hd9JpN/9wZgSs8YsvcG0xmXbgwq0W7JP85aORjkiysn/pS@oP6E8L6usH0zwvyC/wL1MUr3RzGEybu8pi5oS9rlYRiAVa2EGrWw3JLld3AcaCFcYa74qsqjAtoUKUlmOMkxiVgN0bEX9ICNMxcNBEux3SL3iej9IEDgmaeWc2vYbcTwV9E6dLjR3c5Mb1CTfqbO9oqDGD6YLxH/07ke5X0kuxkf3uPl/eVAq/uB8 "JavaScript (Node.js) – Try It Online")
  • ## Some test cases
  • ```
  • 1 + 0 = 1
  • 1 + 1 = 110 // 1 + 1 = 2
  • 1 + 110 = 111 // 1 + 2 = 3
  • 10 + 1 = 11 // -2 + 1 = -1
  • 10 + 1011 = 110101 // -2 + -9 = -11
  • 1010101 + 1110100 = 110011001
  • ```
#2: Post edited by user avatar Moshi‭ · 2021-06-29T23:17:41Z (almost 3 years ago)
Updated output specification
  • ## Challenge
  • Given two negabinary integers (base -2), output [the sum of them in negabinary](https://en.wikipedia.org/wiki/Negative_base#Arithmetic_operations).
  • You may take inputs in any format that makes sense.
  • The following ungolfed example takes in two 0-indexed arrays, with the nth entry being the nth place of each number.
  • ```javascript
  • function addNegabinary(first, second) {
  • let carry = 0;
  • let result = [];
  • let place = 0;
  • while(place < first.length || place < second.length || carry) {
  • let num = (first[place] || 0) + (second[place] || 0) + carry;
  • let bit;
  • switch (num) {
  • case -1: bit = 1; carry = 1; break;
  • case 0: bit = 0; carry = 0; break;
  • case 1: bit = 1; carry = 0; break;
  • case 2: bit = 0; carry = -1; break;
  • case 3: bit = 1; carry = -1; break;
  • }
  • result.push(bit);
  • place++;
  • }
  • return result;
  • }
  • }
  • ```
  • [Try it online!](https://tio.run/##fVPLboMwELzzFXsEhUR2e6vbX@gPIB8cYwKtYyLbNIqafHvqRwCnpUEIrWdnZ1fr4YN9McN1d7Br1dfiem0GxW3XK2B1/S52bNsppk9502ljSzCC96ou4DsD90hhgTOtT/AGiEyQFmaQ1mEVJdmEHiTjYiYe206KPIKvEOQ3UqidbeF8hhGP/ZJEaDf2H6XVsHfCccYqlFLPRQWsII8Sv@GgQ@5Utp2dAXPsLG8hd9JpN/9wZgSs8YsvcG0xmXbgwq0W7JP85aORjkiysn/pS@oP6E8L6usH0zwvyC/wL1MUr3RzGEybu8pi5oS9rlYRiAVa2EGrWw3JLld3AcaCFcYa74qsqjAtoUKUlmOMkxiVgN0bEX9ICNMxcNBEux3SL3iej9IEDgmaeWc2vYbcTwV9E6dLjR3c5Mb1CTfqbO9oqDGD6YLxH/07ke5X0kuxkf3uPl/eVAq/uB8 "JavaScript (Node.js) – Try It Online")
  • ## Some test cases
  • ```
  • 1 + 0 = 1
  • 1 + 1 = 110 // 1 + 1 = 2
  • 1 + 110 = 111 // 1 + 2 = 3
  • 10 + 1 = 11 // -2 + 1 = -1
  • 10 + 1011 = 110101 // -2 + -9 = -11
  • 1010101 + 1110100 = 110011001
  • ```
  • ## Challenge
  • Given two negabinary integers (base -2), output [the sum of them in negabinary](https://en.wikipedia.org/wiki/Negative_base#Arithmetic_operations).
  • You may take inputs in any format that makes sense. However, the output of the function/program must be in the same format as the input
  • The following ungolfed example takes in two 0-indexed arrays, with the nth entry being the nth place of each number.
  • ```javascript
  • function addNegabinary(first, second) {
  • let carry = 0;
  • let result = [];
  • let place = 0;
  • while(place < first.length || place < second.length || carry) {
  • let num = (first[place] || 0) + (second[place] || 0) + carry;
  • let bit;
  • switch (num) {
  • case -1: bit = 1; carry = 1; break;
  • case 0: bit = 0; carry = 0; break;
  • case 1: bit = 1; carry = 0; break;
  • case 2: bit = 0; carry = -1; break;
  • case 3: bit = 1; carry = -1; break;
  • }
  • result.push(bit);
  • place++;
  • }
  • return result;
  • }
  • }
  • ```
  • [Try it online!](https://tio.run/##fVPLboMwELzzFXsEhUR2e6vbX@gPIB8cYwKtYyLbNIqafHvqRwCnpUEIrWdnZ1fr4YN9McN1d7Br1dfiem0GxW3XK2B1/S52bNsppk9502ljSzCC96ou4DsD90hhgTOtT/AGiEyQFmaQ1mEVJdmEHiTjYiYe206KPIKvEOQ3UqidbeF8hhGP/ZJEaDf2H6XVsHfCccYqlFLPRQWsII8Sv@GgQ@5Utp2dAXPsLG8hd9JpN/9wZgSs8YsvcG0xmXbgwq0W7JP85aORjkiysn/pS@oP6E8L6usH0zwvyC/wL1MUr3RzGEybu8pi5oS9rlYRiAVa2EGrWw3JLld3AcaCFcYa74qsqjAtoUKUlmOMkxiVgN0bEX9ICNMxcNBEux3SL3iej9IEDgmaeWc2vYbcTwV9E6dLjR3c5Mb1CTfqbO9oqDGD6YLxH/07ke5X0kuxkf3uPl/eVAq/uB8 "JavaScript (Node.js) – Try It Online")
  • ## Some test cases
  • ```
  • 1 + 0 = 1
  • 1 + 1 = 110 // 1 + 1 = 2
  • 1 + 110 = 111 // 1 + 2 = 3
  • 10 + 1 = 11 // -2 + 1 = -1
  • 10 + 1011 = 110101 // -2 + -9 = -11
  • 1010101 + 1110100 = 110011001
  • ```
#1: Initial revision by user avatar Moshi‭ · 2021-06-28T04:00:36Z (almost 3 years ago)
Add two negabinary integers
## Challenge

Given two negabinary integers (base -2), output [the sum of them in negabinary](https://en.wikipedia.org/wiki/Negative_base#Arithmetic_operations).

You may take inputs in any format that makes sense.

The following ungolfed example takes in two 0-indexed arrays, with the nth entry being the nth place of each number.

```javascript
function addNegabinary(first, second) {
    let carry = 0;
    let result = [];

    let place = 0;
    while(place < first.length || place < second.length || carry) {
        let num = (first[place] || 0) + (second[place] || 0) + carry;
        let bit;
        switch (num) {
            case -1: bit = 1; carry = 1; break;
            case 0: bit = 0; carry = 0; break;
            case 1: bit = 1; carry = 0; break;
            case 2: bit = 0; carry = -1; break;
            case 3: bit = 1; carry = -1; break;
        }
        result.push(bit);
        place++;
    }
    return result;
}
}
```

[Try it online!](https://tio.run/##fVPLboMwELzzFXsEhUR2e6vbX@gPIB8cYwKtYyLbNIqafHvqRwCnpUEIrWdnZ1fr4YN9McN1d7Br1dfiem0GxW3XK2B1/S52bNsppk9502ljSzCC96ou4DsD90hhgTOtT/AGiEyQFmaQ1mEVJdmEHiTjYiYe206KPIKvEOQ3UqidbeF8hhGP/ZJEaDf2H6XVsHfCccYqlFLPRQWsII8Sv@GgQ@5Utp2dAXPsLG8hd9JpN/9wZgSs8YsvcG0xmXbgwq0W7JP85aORjkiysn/pS@oP6E8L6usH0zwvyC/wL1MUr3RzGEybu8pi5oS9rlYRiAVa2EGrWw3JLld3AcaCFcYa74qsqjAtoUKUlmOMkxiVgN0bEX9ICNMxcNBEux3SL3iej9IEDgmaeWc2vYbcTwV9E6dLjR3c5Mb1CTfqbO9oqDGD6YLxH/07ke5X0kuxkf3uPl/eVAq/uB8 "JavaScript (Node.js) – Try It Online")

## Some test cases

```
1 + 0 = 1
1 + 1 = 110        // 1 + 1 = 2
1 + 110 = 111      // 1 + 2 = 3
10 + 1 = 11        // -2 + 1 = -1
10 + 1011 = 110101 // -2 + -9 = -11

1010101 + 1110100 = 110011001
```