Bitwise operators

S

Santhosh

Hi to all,
How the individual digits of a number can be obtained using the
bitwise operators alone.Is it possible to do it ?

If we have n = 34

Result has to be 3,4.

Thanks a billion for your reply in advance.
 
W

Walter Roberson

Santhosh said:
How the individual digits of a number can be obtained using the
bitwise operators alone.Is it possible to do it ?
If we have n = 34
Result has to be 3,4.

This is obviously an artificial question such as for a homework
assignment or an interview question, so I decline to demonstrate.

I would recommend, though, being more explicit as to which
are the "bitwise operators" that are to be considered. For example
would a solution be ruled out if it had loops or conditional
tests? Is comparision to 0 a bitwise operation ?
 
T

Tomás Ó hÉilidhe

 If we have n  = 34

Result has to be 3,4


Ever heard of mathematics? Number systems?

A typical number system consists of:
1) Radix = the amount of different symbols
2) The actual pictures that represent the symbols

Let's take the "octal" number system:
1) Radix = 8
2) Symbols = 0 1 2 3 4 5 6 7

If you want to represent a number that is greater than or equal to
radix, then you need more than one digit. So in octal, the number
eight is written as:
10
And nine is written as:
11

Here, the "11" is equal to:
1 multiplied by (8 to the power of 1)
+1 multiplied by (8 to the power of 0)

Try another octal number: 2763
2 multiplied by (8 to the power of 3)
+7 multiplied by (8 to the power of 2)
+6 multiplied by (8 to the power of 1)
+3 multiplied by (8 to the power of 0)

Try think now, if you had a number in decimal such as 34, then what
would you do to it to get the first digit and the second digit? I'll
give you a clue, it involves using the radix, i.e. 10, in conjunction
with a mathematical operation such as division.
 
P

Peter Nilsson

Santhosh said:
Hi to all,
How the individual digits of a number can be obtained using the
bitwise operators alone.Is it possible to do it ?

Yes, but that isn't a question about C, merely a question on
algorithms. Google for BCD.
 
W

Walter Roberson

Santhosh wrote:
Yes, but that isn't a question about C, merely a question on
algorithms. Google for BCD.

In a way, it is a question about C, as "bitwise operators" would
have to be interpreted in the context of C: the exact operators
which are considered "bitwise" operators will make a difference
to whether such an algorithm can be constructed.

Turing equivilence only works for "sufficiently powerful"
computation systems, and the four operators ~ ^ & | alone
are not "sufficiently powerful" for to be able to generate
any arbitrary algorithm.
 
A

Antoninus Twink

Turing equivilence only works for "sufficiently powerful" computation
systems, and the four operators ~ ^ & | alone are not "sufficiently
powerful" for to be able to generate any arbitrary algorithm.

They are sufficiently powerful to generate all the operations performed
by your favorite ALU.
 
J

Johannes Bauer

Richard said:
Well, I agree that you need a "jump operator" of some kind, and a "test
operator".

Not for this problem. The data type which is input has a finite size,
therefore enumeration is possible, yielding an extremely huge and
complex boolean expression that does not need one single jump or test.
Probably far too huge to be compiled by any compiler, but, hey...

Regards,
Johannes
 
J

Johannes Bauer

Johannes said:
Not for this problem. The data type which is input has a finite size,
therefore enumeration is possible, yielding an extremely huge and
complex boolean expression that does not need one single jump or test.
Probably far too huge to be compiled by any compiler, but, hey...

For demonstration purposes, I've created a program which enumerates the
numbers from 0-99 and always does output the 10^1 digit:

http://nopaste.org/p/ajyaa3Ipe

It's mighty impressive ;-)

Regards,
Johannes
 
O

onkar.n.mahajan

Hi to all,
How the individual digits of a number can be obtained using the
bitwise operators alone.Is it possible to do it ?

If we have n = 34

Result has to be 3,4.

Thanks a billion for your reply in advance.

Indians always ask interview Questions ! :)
 
O

onkar.n.mahajan

Hi to all,
How the individual digits of a number can be obtained using the
bitwise operators alone.Is it possible to do it ?

If we have n = 34

Result has to be 3,4.

Thanks a billion for your reply in advance.

This is a comp.lang.c forum - "not to discuss interview Qs related to
algos here" , we are here to discuss Qs related to C programming
language !
 
S

santosh

This is a comp.lang.c forum - "not to discuss interview Qs related to
algos here" , we are here to discuss Qs related to C programming
language !

No, the question is perfectly legitimate[1]. That it may be
an "interview question" or a homework question is irrelevant. Of course
questions of this type often don't elicit the type of response that the
OP would like, but that's a different matter.

1. Always assuming that the OP wants a C based answer. Otherwise it's
not topical here and he should probably post to comp.programming.
 
R

rahul

Hi to all,
How the individual digits of a number can be obtained using the
bitwise operators alone.Is it possible to do it ?

If we have n = 34

Result has to be 3,4.

Thanks a billion for your reply in advance.

We generally use % and / to get the individual digits.
#include <stdio.h>
#include <stdlib.h>

int
main(void) {
int num = 34;
int digit = 0;
while (num != 0) {
digit = num % 10;
num /= 10;
printf("%d\n", digit);
}
return 0;
}

The result will be 4, 3 as it starts separating from the unit's place.
You are not clear on where do you want to use the bitwise operator.
 
D

den2k

I do not think it is possible using only bitwise operators because
they work on a base-2 numbering system whereas you want to know the
_decimal_ digits.
 
D

den2k

den2k said:
Consider 34 / 10 (which we need to do if we're going to get the result 3
and remainder 4). 34 is 100010 in binary, and ten is 1010. Observe long
division in binary, noting that subtraction is done with ^ & << and the
two's complement, comparison can be done by subtraction, isolating the
bits necessary for the trial divisions can be done using shift and AND,
and "bringing down a bit" can be done with shift and OR:

1010)100010
1010 <---- Subtrahend <= minuend? No.

_0000
1010)100010
1010 <---- Subtrahend <= minuend? Yes. Subtract.

_00001
1010)100010
1010
---
111 <----- bring down a bit

_00001
1010)100010
1010
---
1110
1010 <---- Subtrahend <= minuend? Yes. Subtract.

_000011 <---- Result: 3
1010)100010
1010

Whoops I unconsciously thougt to a sigle bitwise operator.. and I
didn't know so well the use of bitwise Operators in order to do the
basilar operations. Thanks a lot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

bitwise operators 14
bitwise operators(info) 21
Math python question 10
bitwise decimal operators 2
Bitwise complement 4
Bitwise 2k9 0
Bitwise Operators: Aplication 5
Bitwise operators. 26

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top