4

How do I extract digits from an integer using bitwise operator. i.e. I want to extract 234 in the integer n=123456 I know this is possible using and, or, xor bitwise operator but I'm not sure How? thnx

8
  • 9
    Not in base 10... Commented Feb 23, 2012 at 4:43
  • 5
    @Mysticial : I would have said it is only possible in base b10. Commented Feb 23, 2012 at 4:44
  • 2
    And I thought I was the geek... Commented Feb 23, 2012 at 4:47
  • Why such an artificial restriction to bitwise operators? Is this homework? Are bit-shift operators considered ok? Commented Feb 23, 2012 at 4:49
  • 1
    Generelly I don't think one should start reimplement existing functions, except for two reasons: There are bugs in it (but then you should rather make a bugreport), or you are really sure you can implement it faster/more robust/more scaleable, because for example you examined the implementation. Commented Feb 23, 2012 at 10:30

4 Answers 4

5

If you don't want bitwise at this point, I believe you can extract individual digits like this:

int digitAtPos(int number, int pos) { return ( number / (int)pow(10.0, pos-1) ) % 10; } 

I'm not sure if this is the fastest/most-efficient way, but it works.

Stitching the digits back together should be easy as well, just multiply by increasing powers of ten.

If anyone has a better solution, please tell.

Sign up to request clarification or add additional context in comments.

3 Comments

Good, now all you have to do is to replace all the operations above with bit-wise operations.
How would you go about doing that for the pow() function? I guess you could do repeated multiplication, but would that take longer? And how do you make bitwise work with base 10?
This is like saying you can squish an ant with a hammer by using the hammer to build an ant-squishing machine. If dividing by ten and mode 10 are allowed, the problem is trivial to solve. Using pow and floating point math is silly.
3

Since this is tagged as homework, i'm only going to explain basic concepts, I'm not actually going to produce an answer.

You use bitwise operations to extract individual bits of a base 2 number. So if your number is 10110101, and you want to get the 0101 at the end, you would have to set up a mask, and then do a binary anding to filter out the unimportant digits. Your mask should look something like 00001111. When you and them together, only the digits you want are remaining.

10110101 & 00001111 == 00000101

Comments

0

You can extract the digits one by one using long division. You will need to implement the primitives you'll need to do long division.

Exactly how you do it depends on the precise set of operations you're allowed to use. For example, if you're not allowed to add, you'll have to build your own add operation. If you're not allowed to subtract, you'll need to build a subtract with XOR, increment, and add.

Once you have a division primitive, it's easy. Successive divisions by 1010b will extract the digits.

1 Comment

Note: The link is dead as of 2025-03-06, and archive.org never caught a snapshot. Op, are there any good alternative links out there?
-1

Convert the int to binary. Move the digit at position you need to the leftmost or rightmost. Bitwise AND with number having all zeros and just one 1 at the leftmost or rightmost according to the shifting we have performed. re covert the binary back to decimal.

int pos; cout<<"ENter position"; cin>>pos; int temp = pos; while(temp!=0) { //right shift the number temp--; } 

Maybe taking a look at this might help: Extracting bits with bitwise operators

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.