当前位置:网站首页>How to judge whether a number is odd or even?
How to judge whether a number is odd or even?
2022-07-27 02:25:00 【Current affairs reader hjj】
List of articles
How to judge whether a number is odd or even ?
Judge whether a number is odd or even , The easiest thing to think of is right 2 Remainder .
Method 1
int number = 11;
if (number % 2 == 0) {
System.out.println("Number For the even ");
} else {
System.out.println("Number It's odd ");
}
Output results :Number It's odd
int number = 10;
if (number % 2 == 0) {
System.out.println("Number For the even ");
} else {
System.out.println("Number It's odd ");
}
Output results :Number For the even
Method 2
int number = 11;
if ((number & 1) == 0) {
System.out.println("Number For the even ");
} else {
System.out.println("Number It's odd ");
}
Output results :Number It's odd
int number = 10;
if ((number & 1) == 0) {
System.out.println("Number For the even ");
} else {
System.out.println("Number It's odd ");
}
Output results :Number For the even
What is the principle of method 2 ?
In the computer , Data is stored in complementary binary .
The lowest of even numbers must be 0.
The lowest of odd numbers must be 1.
So if you want to judge whether this number is odd or even , Just use this number to bitwise AND 1 That's all right. .
If the result is 0, So this number is even , If the result is 1, Then this number is odd .
My learning forum
HandsomeForum: use Java Prepared Learning Forum , Build our own circle !(http://huangjunjie.vip:66)
The article links :http://huangjunjie.vip:66/blog/read/pahjl1d26w0f4u1ogx
边栏推荐
- Constant knowledge explanation of C language
- [C language programming] branch structure
- 三个整数从大到小排序(详细介绍多种方法)
- (前缀和/思维)Codeforces Round #806 (Div. 4)F. Yet Another Problem About Pairs Satisfying an Inequality
- Detailed source code of golang bufio reader
- Lecture 3 - GPIO input / output library function usage and related routines
- Codeforces Round #807 (Div. 2), problem: (C) Mark and His Unfinished Essay
- Self introduction and planning about programming
- 光光光仔的CSDN之旅
- CF 1333C Eugene and an array
猜你喜欢

全连MGRE与星型拓扑MGRE

Esp8266wi fi access cloud platform

C language -- nesting of relational and logical operators, if statements, switch statements, and branch structures

HCIA (network elementary comprehensive experimental exercise)

Hcip bidirectional republication and routing strategy

多线程中 synchronized 锁升级的原理是什么?

Codeforces Round #807 (Div. 2), problem: (C) Mark and His Unfinished Essay

CAN总线通信应用

Brief introduction of VLAN principle and specific experimental configuration

HCIA Basics (1)
随机推荐
静态路由实验配置
数字集成电路:MOS管器件章(二)
记录第N次SQL异常
NB-IOT接入云平台
(史上最详细)Codeforces Round #805 (Div. 3)E. Split Into Two Sets
勤写标兵——云小井
Detailed source code of golang bufio reader
HCIA (network elementary comprehensive experimental exercise)
Codeforces Round #796 (Div. 2), problem: (1688C) Manipulating History
HCIP oSPF综合实验
Codeforces Round #809 (Div. 2), problem: (C) Qpwoeirut And The City
Educational Codeforces Round 132 (Rated for Div. 2), problem: (D) Rorororobot
RS-485总线通信应用
HCIP-第四天-OSPF路由协议
Esp8266wi fi access cloud platform
C语言的常数知识讲解
About unsafe problems such as fopen and strError encountered in vs2022 or advanced version running environment
Educational Codeforces Round 132 (Rated for Div. 2), problem: (D) Rorororobot
MySQL课程1.简单命令行--简单记录 欢迎补充纠错
Find a specific number in an ordered array