鸟语天空
判断是否是2的阶次方
post by:追风剑情 2019-3-15 23:31

示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TSortTest
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = -2; i <= 16; i++)
                Console.WriteLine("{0}={1}", i, IsTwoPower(i));

            Console.Read();
        }

        // 判断x是否为2的阶次方
        public static bool IsTwoPower(int x)
        {
            return (x > 0) && (x & (x - 1)) == 0;
        }
    }
}

运行测试
111.png

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容