[Microsoft] O(1) Check Power of 2

本文介绍了一种使用O(1)时间复杂度的方法来检查一个整数是否为2的幂次方。利用位运算的特点,通过观察n与n-1的二进制形式,如果n为2的幂,则n与n-1进行按位与运算结果为0。

Using O(1) time to check whether an integer n is a power of 2.

Example

For n=4, return true;

For n=5, return false;

思路:2的幂次方(2^n)的二进制表达法只有一位为1,其他位都为0,而(2^n-1)则是和(2^n)对应位置正好不一样

           例如8的二进制1000,7的二进制0111

           因为二者可以进行按位与运算,因为按位与运算只有两位同时为1时,结果才为1,否则为0

注意:参加运算的两个数据,按二进制位进行“与”运算,运算规则:0&0=0;   0&1=0;    1&0=0;     1&1=1

class Solution {
    /*
     * @param n: An integer
     * @return: True or false
     */
    public boolean checkPowerOf2(int n) {
        if(n<=0){
            return false;
        }
        
        return (n&(n-1))==0?true:false;     
    }
};
关于位运算: 点击打开链接
### Step-by-Step Guide to Complete PC Hardware Assembly and Software Installation #### **1. Prepare the Components** Ensure all hardware is available: - Case, Motherboard, CPU, RAM, GPU (if not integrated), Storage (SSD/HDD), PSU, Cooling (fan or liquid cooler) - Tools: Screwdriver, anti-static wrist strap #### **2. Install CPU into the Motherboard** - Open the CPU socket lever on the motherboard. - Align the CPU (match triangle marker) and gently place it into the socket. - Secure the socket lever. #### **3. Install CPU Cooler** - Apply thermal paste (if not pre-applied). - Mount the cooler according to its design (clip, bracket, or screws). - Connect the fan power cable to the **CPU_FAN** header. #### **4. Install RAM** - Open the clips on the memory slots. - Align the notch on the DDR4/DDR5 module with the slot. - Press firmly until clips snap into place. #### **5. Mount the Motherboard into the Case** - Install standoffs in the case (if not pre-installed). - Place I/O shield into the back of the case. - Lower the motherboard onto the standoffs and secure with screws. #### **6. Install Power Supply (PSU)** - Place the PSU into the case’s PSU bay (usually bottom-rear). - Secure with screws. - Route main **24-pin ATX** and **8-pin CPU power** cables to the motherboard. #### **7. Install Storage (SSD/HDD)** - Mount drive in a drive bay (2.5"/3.5"). - Connect **SATA data cable** to the motherboard and **SATA power** from PSU. - For M.2 SSD: Insert into M.2 slot, secure with screw. #### **8. Install Graphics Card (GPU)** - Remove PCIe slot covers on the case. - Insert GPU into **PCIe x16** slot. - Secure with screws; connect required **PCIe power cables** from PSU. #### **9. Connect Front Panel Headers** - Connect **Power Switch, Reset, HDD LED, Power LED** using the motherboard manual for pin layout. - Connect **Front USB and Audio** headers to their respective pins. #### **10. Cable Management** - Route cables through cutouts. - Use zip ties to secure and improve airflow. #### **11. Double-Check All Connections** - Ensure all power cables (main, CPU, GPU, drives) are securely connected. - Verify RAM, GPU, and CPU are seated properly. #### **12. Power On (First Boot)** - Connect monitor, keyboard, and power cord. - Press power button. - Check for POST (Power-On Self-Test) — screen should show BIOS/UEFI. #### **13. Install Operating System (e.g., Windows 10/11)** - Create bootable USB using another PC and Microsoft Media Creation Tool. - Insert USB, boot from it (press $F12$/$DEL$/EFI boot menu). - Follow setup: select language, partition disk, install OS. #### **14. Install Drivers and Updates** - Install motherboard drivers (chipset, LAN, audio) from manufacturer’s website or included USB. - Update GPU drivers (NVIDIA/AMD/Intel). - Run Windows Update. #### **15. Final Checks and Optimization** - Enter BIOS ($DEL$ key at boot): confirm XMP profile enabled (for RAM speed). - Set boot order (SSD first). - Install antivirus and essential software. > Your PC is now fully assembled and ready for use. 翻译为中文
最新发布
09-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值