Jack
Jack

Reputation: 16724

How to remove a number from another number using bitwise operators?

How do I remove for example 2 from 123 and returns 13, by using bitwise operators? I have no idea how to do this.. it's possible? Thanks in advance.

Upvotes: 1

Views: 1619

Answers (4)

Jason Larke
Jason Larke

Reputation: 5609

What other cases are there? It may be possible to generalize this at an integer level if there is some sort of pattern, otherwise you're really just looking at string replacement.

The 2 in 123 is actually 2E1 (10100), and the 2 in 1234 would be 2E2 (11001000) neither of which are related to 2 (10), at least in bitwise form. Also, the "number" on the right side of the removed number would need to be added to the number on the left side of the removed number / 10.

i.e, to go from 123 to 13:

Located "2".
Number on left (x): 100
Number on right (y): 3
y + (x / 10) = 13

and to go from 1324 to 134

Located "2"
Number on left (x): 1300
Number on right (y): 4
y + (x / 10) = 134

Unless there's some pattern (i.e you know what positions the numbers are in), you'll just have to .ToString() the number, then do a .Replace("2", ""), before doing an int.Parse() on the result.

EDIT: Someone upvoted this answer and I realised my previous implementation was unnecessarily complicated. It is relatively straightforward to 'iterate' over the digits in a base-10 number and shouldn't require recursion.

New solution below, performance is better but this is a huge micro-optimisation:

static int OmitDigit(int number, int digit) {
    var output = 0;
    var multiplier = 1;
    
    while (number > 0) {
        var n = number % 10;
        number /= 10;
        
        if (n != digit) {
            output += (n * multiplier);
            multiplier *= 10;
        }
    }
    
    return output;
}

Result: 1554443

Upvotes: 2

zmbq
zmbq

Reputation: 39023

This is very awkward, but if you really do must have bitwise operations only, I suggest you convert the number to BCD. Once in BCD, you basically have an hexadecimal numbers with digits between 0 and 9, so removing a digit is very simple. Once you're done, convert back to binary.

I don't believe anybody would want to do that.

Upvotes: 0

Dave
Dave

Reputation: 11162

Since we're working with base 10 numbers, manipulation in base 2 is ugly to say the least. Using some math, removing the nth digit from k, and shifting over is

(k/pow(10,n))*pow(10, n-1) + k%pow(10, n-1)

In base 2, the << and >> operator acts like multiplying by a pow(2, n), and & with a mask does the work of %, but in base 10 the bits don't line up.

Upvotes: 0

evanmcdonnal
evanmcdonnal

Reputation: 48114

What you're suggesting is possible, but wouldn't really make sense to do. Below are the values representations in bits (only showing relevant bits, everything further left is a zero):

2: 000010 || 123: 1111011 || 13: 001101

There's no logical way to change 123 into 13 with bitwise operations. It would better to turn it into a string or character array, remove the two then cast it back to an int.

Upvotes: 3

Related Questions