当前位置:网站首页>Leetcode does not add, subtract, multiply, divide, and calculate the number of 1 in binary
Leetcode does not add, subtract, multiply, divide, and calculate the number of 1 in binary
2022-07-24 06:25:00 【Happy and at ease】

You cannot use addition, subtraction, multiplication and division , Then we can consider what we learned in introduction to computer ^( Exclusive or ) operation as well as &( And operation )

According to the above ideas The code is as follows
class Solution {
public:
int Add(int num1, int num2) {
while(num2!=0){
int sum=num1^num2;
int add=(unsigned int )(num1&num2)<<1;
num1=sum;
num2=add;
}
return num1;
}
};The second topic , It is still done by bit operation for example << Operator is to move binary bits to the left by one bit

In binary numbers 1 The number of , We learned that Any number and decimal number 1 Meet each other , If the mantissa of this number is 0 be , The result is 0, If mantissa is 1 The result is 1
We can pass the 1 Of 32 position 2 Hexadecimal shift left , It can judge the given number 32 Whether the number of digits is 1 The situation of
The code is as follows
class Solution {
public:
int NumberOf1(int n) {
int res=0;
for(int i=0;i<=31;i++){
if((n&(1<<i))!=0)
res++;
}
return res;
}
};边栏推荐
- Leetcode剑指offer JZ9 双栈实现队列
- Kernel pwn 基础教程之 Heap Overflow
- Do not rent a server, build your own personal business website (how to buy a domain name)
- LuckyFrameWeb测试平台(一款支持接口自动化、WEB UI自动化、APP自动化,并且支持分布式测试的全纬度免费开源测试平台)
- IP class notes (5)
- 简单三步快速实现内网穿透
- IP lesson summary (3)
- 力扣986.区间列表的交集
- Openpose2d transform 3D pose recognition
- IP notes (9)
猜你喜欢

Sorting of common AR and MR head mounted display devices

IP job (6)

How to build a website full of ritual sense and publish it on the public website 1-2

IP笔记(12)

进行挂载永久挂载后无法开机

ue4换装系统 1.换装系统的基本原理

leetcode剑指offer JZ23:链表中环的入口节点

项目上复盘引导问题清单

IP class notes (4)

The public network uses Microsoft Remote Desktop remote desktop to work remotely at any time
随机推荐
【226】wireshark的参数使用说明
服务器硬件及RAID配置实战
UE4 replacement system 3. Final results
IP notes (7)
常见十大漏洞总结(原理、危害、防御)
[226] instructions for Wireshark parameters
Flink restart policy
Data set and pre training model
【251】常见的测试工具
IP课(OSPF)综合实验
常用工作方法总结(7S、SWOT分析、PDCA循环、SMART原则、6W2H、时间管理、WBS、二八原则)
Unity (III) three dimensional mathematics and coordinate system
How to build a website full of ritual sense and publish it on the public website 1-2
MySQL从基础到入门到高可用
Dameng database_ Small matters needing attention during use
IP作业(2)RIP
Use intranet penetration to realize public network access to the Intranet
一个测试经理/测试主管/测试总监的工作总结
【217】#!/usr/bin/env 的意义
Getting started with Lunix commands - user and file permissions (Chmod details)