473,467 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Bitwise operators

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.
Jun 27 '08 #1
16 2970
In article <39**********************************@p39g2000prm. googlegroups.com>,
Santhosh <sa****************@gmail.comwrote:
>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 ?
--
"There is no greater calling than to serve your fellow men.
There is no greater contribution than to help the weak.
There is no greater satisfaction than to have done it well."
-- Walter Reuther
Jun 27 '08 #2
On Jun 18, 10:32*pm, Santhosh <santhoshvenkat1...@gmail.comwrote:
*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.
Jun 27 '08 #3
Santhosh wrote:
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.

--
Peter
Jun 27 '08 #4
In article <01**********************************@w34g2000prm. googlegroups.com>,
Peter Nilsson <ai***@acay.com.auwrote:
>Santhosh wrote:
>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.
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.
--
"History is a pile of debris" -- Laurie Anderson
Jun 27 '08 #5
On 18 Jun 2008 at 22:09, Walter Roberson wrote:
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.

Jun 27 '08 #6
Walter Roberson said:

<snip>
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.
Well, I agree that you need a "jump operator" of some kind, and a "test
operator". But it's amazing what you /can/ do with the ~ ^ & | ><<
collection (you missed the shifts, which is presumably just an oversight).
^ gives you a half-adder. & and << give you the other half, handing you
full addition on a plate. Using two's complement gives you subtraction
almost for free. With those, you can do multiplication, division, and mod.
And with those five arithmetic operators in place, you can do just about
anything that can be done.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #7
Richard Heathfield schrieb:
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

--
"Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
reicht zu wissen, daß andere es besser können und andere es auch
besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
in de.sci.electronics <47***********************@news.freenet.de>
Jun 27 '08 #8
Johannes Bauer schrieb:
Richard Heathfield schrieb:
>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...
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

--
"Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
reicht zu wissen, daß andere es besser können und andere es auch
besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
in de.sci.electronics <47***********************@news.freenet.de>
Jun 27 '08 #9
Johannes Bauer said:
Richard Heathfield schrieb:
>Well, I agree that you need a "jump operator" of some kind, and a "test
operator".

Not for this problem.
Quite. My comment was made with regard to Walter's more general claim that
"the four operators ~ ^ & | alone are not "sufficiently powerful" for to
be able to generate any arbitrary algorithm".

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #10
In article <n9************@joeserver.homelan.net>,
Johannes Bauer <df***********@gmx.dewrote:
>For demonstration purposes, I've created a program which enumerates the
numbers from 0-99 and always does output the 10^1 digit:
(using bitwise operators)
>http://nopaste.org/p/ajyaa3Ipe
>It's mighty impressive ;-)
Definitely a labour of love!
--
"When a scientist is ahead of his times, it is often through
misunderstanding of current, rather than intuition of future truth.
In science there is never any error so gross that it won't one day,
from some perspective, appear prophetic." -- Jean Rostand
Jun 27 '08 #11
On Jun 19, 2:32 am, Santhosh <santhoshvenkat1...@gmail.comwrote:
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 ! :-)
Jun 27 '08 #12
On Jun 19, 2:32 am, Santhosh <santhoshvenkat1...@gmail.comwrote:
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 !
Jun 27 '08 #13
on*************@gmail.com wrote:
On Jun 19, 2:32 am, Santhosh <santhoshvenkat1...@gmail.comwrote:
>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 !
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.

Jun 27 '08 #14
On Jun 19, 2:32 am, Santhosh <santhoshvenkat1...@gmail.comwrote:
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.
Jun 27 '08 #15
On 19 Giu, 08:35, rahul <rahulsin...@gmail.comwrote:
On Jun 19, 2:32 am, Santhosh <santhoshvenkat1...@gmail.comwrote:
Hi to all,
How the individual digits of a number can be obtained using the
bitwise operators alone.Is it possible to do it ?
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.
Jun 27 '08 #16
den2k said:
On 19 Giu, 08:35, rahul <rahulsin...@gmail.comwrote:
>On Jun 19, 2:32 am, Santhosh <santhoshvenkat1...@gmail.comwrote:
Hi to all,
How the individual digits of a number can be obtained using the
bitwise operators alone.Is it possible to do it ?

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.
We have already discussed the fact that addition can be done using the ^,
&, and << operators (XOR for add-without-carry, AND and LSH for obtaining
and moving the carry, repeat until no carry). Given addition, subtraction
is easy - simply take two's complement and add.

Given subtraction, division becomes not only possible but even easy.
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
---
1110
1010
---
100 <---- Remainder: 4

Yes, Virginia, you *can* divide by ten, even if you're thinking in binary.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Michael B. Trausch | last post by:
I have a question regarding bitwise operators, I've been trying to figure this out for about two days now, and I just can't seem to get it. What I'm trying to do is use a variable to hold a bitmask...
11
by: Randell D. | last post by:
Why would one use bitwise operators? I can program in various languages in some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise operators before, but never understood why...
4
by: Mike Hodkin | last post by:
As a beginning student of C++, books reference "bitwise operators" and give brief examples, but I have not read a good explanation of what they are used for. One reference mentioned that they are...
6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
37
by: James Radke | last post by:
Hello, I found some code that I could use in my application on the web, but it is written in C#, and I would like to convert it to VB. And I am having problems with one section. Is there...
5
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what...
2
by: Mark Rae | last post by:
Hi, This isn't *specifically* an ASP.NET question, so I've also posted it in the ADO.NET group - however, it's not too far off-topic... Imagine a SQL Server 2005 database with a table with an...
29
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.