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
- 9Not in base 10...Mysticial– Mysticial2012-02-23 04:43:41 +00:00Commented Feb 23, 2012 at 4:43
- 5@Mysticial : I would have said it is only possible in base b10.J.N.– J.N.2012-02-23 04:44:52 +00:00Commented Feb 23, 2012 at 4:44
- 2And I thought I was the geek...Mysticial– Mysticial2012-02-23 04:47:12 +00:00Commented Feb 23, 2012 at 4:47
- Why such an artificial restriction to bitwise operators? Is this homework? Are bit-shift operators considered ok?Tony Delroy– Tony Delroy2012-02-23 04:49:33 +00:00Commented Feb 23, 2012 at 4:49
- 1Generelly 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.AlexS– AlexS2012-02-23 10:30:43 +00:00Commented Feb 23, 2012 at 10:30
4 Answers
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.
3 Comments
pow() function? I guess you could do repeated multiplication, but would that take longer? And how do you make bitwise work with base 10?pow and floating point math is silly.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
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
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