Home » SQL & PL/SQL » SQL & PL/SQL » No of 1's in a binary string
No of 1's in a binary string [message #297427] Thu, 31 January 2008 08:47 Go to next message
dba_bng
Messages: 13
Registered: April 2007
Junior Member
Have the following requirement.

Input : 6 (Binary representation - 0110)
Output : 2 (No. of 1's in the input's binary string)

Input : 10 (Binary representation - 1010)
Output : 2 (No. of 1's in the input's binary string)

Input : 14 (Binary representation - 1110)
Output : 3 (No. of 1's in the input's binary string)

What would be the most efficient way to implement the above.
Re: No of 1's in a binary string [message #297429 is a reply to message #297427] Thu, 31 January 2008 09:09 Go to previous messageGo to next message
pablolee
Messages: 2834
Registered: May 2007
Location: Scotland
Senior Member
take the length of the string and subtract the length of the string where the 1's have been replaced.
Use functions LENGTH and REPLACE
Re: No of 1's in a binary string [message #297431 is a reply to message #297429] Thu, 31 January 2008 09:17 Go to previous messageGo to next message
sudhaputtagunta
Messages: 7
Registered: January 2007
Junior Member
Can u please explain with example
Re: No of 1's in a binary string [message #297434 is a reply to message #297431] Thu, 31 January 2008 09:47 Go to previous messageGo to next message
S.Rajaram
Messages: 1027
Registered: October 2006
Location: United Kingdom
Senior Member
check this out.
http://www.orafaq.com/forum/m/260237/94420/?srch=length%28replace%28#msg_260237

Regards

Raj
Re: No of 1's in a binary string [message #297436 is a reply to message #297434] Thu, 31 January 2008 10:10 Go to previous messageGo to next message
dba_bng
Messages: 13
Registered: April 2007
Junior Member
That's not the requirement...
Re: No of 1's in a binary string [message #297439 is a reply to message #297436] Thu, 31 January 2008 10:33 Go to previous messageGo to next message
Michel Cadot
Messages: 64103
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:
That's not the requirement...

This is not a correct way to answer to someone that tries to help you.
You should explain the requirements instead, even if YOU think it is clear.

As I'm in a good mood this afternoon, I will help you:
SQL> col bin format a10
SQL> def N=6
SQL> select reverse(max(replace(sys_connect_by_path(mod(trunc(&N/power(2,level-1)),2),' '),' ',''))) bin,
  2         sum(mod(trunc(&N/power(2,level-1)),2)) Ones
  3  from dual
  4  connect by level <= 10
  5  /
BIN         ONES
---------- -----
0000000110     2

1 row selected.

SQL> def n=10
SQL> /
BIN         ONES
---------- -----
0000001010     2

1 row selected.

SQL> def n=14
SQL> /
BIN         ONES
---------- -----
0000001110     3

1 row selected.

Regards
Michel
Re: No of 1's in a binary string [message #298045 is a reply to message #297439] Mon, 04 February 2008 14:56 Go to previous messageGo to next message
Michel Cadot
Messages: 64103
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Thanks for your feedback, Deepak.

See http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1284001741333#652585600346486485

Regards
Michel
Re: No of 1's in a binary string [message #298046 is a reply to message #298045] Mon, 04 February 2008 15:05 Go to previous messageGo to next message
Littlefoot
Messages: 20891
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Michel Cadot wrote on Mon, 04 February 2008 21:56
Thanks for your feedback, Deepak.

./fa/1600/0/ Deepak? Feedback? Am I missing something?

[EDIT] Ah, the AskTom Deepak ...

[Updated on: Mon, 04 February 2008 15:06]

Report message to a moderator

Re: No of 1's in a binary string [message #298047 is a reply to message #298046] Mon, 04 February 2008 15:18 Go to previous messageGo to next message
Michel Cadot
Messages: 64103
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
And the Deepak in dba_bng's profile.

Curiously Tom gave the same answer than pablolee and S.Rajaram but Deepak did not reply "That's not the requirement..." to Tom. Really strange, isn't it?

Regards
Michel
Re: No of 1's in a binary string [message #298094 is a reply to message #297439] Tue, 05 February 2008 00:28 Go to previous message
dba_bng
Messages: 13
Registered: April 2007
Junior Member
Thanks a lot Michel for your help.
Previous Topic: ORA-06502: PL/SQL: numeric or value error using Bulk
Next Topic: 20 million table partition
Goto Forum:
  


Current Time: Sun Dec 04 02:54:04 CST 2016

Total time taken to generate the page: 0.07737 seconds