Bit operation exercise: Calculate the majority of large numbers and the number of 1 in binary numbers

Source: Internet
Author: User
Code

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< Html Xmlns = "Http://www.w3.org/1999/xhtml" >
  < Head >
< Title > New Document </ Title >
< Meta Name = "Generator" Content = "Editplus"   />
< Meta Name = "Author" Content = ""   />
< Meta Name = "Keywords" Content = ""   />
< Meta Name = "Description" Content = ""   />
  </ Head >

< Body >
< Script Type = "Text/JavaScript" Language = "JavaScript" >
/*
Number of consumers
*/
Function Getmaxnum ()
{
If ( Arguments. Length <= 1 )
{
Return Arguments [ 0 ];
}
VaR ARG = Arguments;
VaR Max = Arg [ 0 ];
VaR I = 1 ;
While (I < Arg. length)
{
// A number is represented by 32 bits, and the highest bits is the symbol (0 is positive and 1 is negative). Therefore, the two bits are subtracted, and the result is shifted to 31 bits, retaining the symbol bit.
// It is followed by the sign bit and Phase 1, and 0 is positive and 1 is negative.
Max = (Max - Arg [I]) > 31 ) & 1 ) = 0 ? MAX: Arg [I];
++ I;
}
Return Max;
}
Function Get1num (Num)
{
VaR Count = 0 ;
While (Num > 0 )
{
// The binary value of A number indicates the number of 1.AlgorithmIs: this number minus 1, and then with its own phase, this cycle until the number is less than or equal to 0. The number of cycles is 1.
Num = (Num - 1 ) & Num;
Count ++ ;
}
Return Count;
}

Document. Write ( " The large numbers in 10,-17.23, and 12 are: " + Getmaxnum ( 10 , - 2 , 15 , 17.23 , 13 , 12 ));
Document. Write ( " <Br/> in the binary representation of 7, the number of 1 is: " + Get1num ( 7 ));
</ Script >
  </ Body >
</ Html >

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.