惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
Google DeepMind News
Google DeepMind News
S
SegmentFault 最新的问题
Project Zero
Project Zero
D
DataBreaches.Net
I
InfoQ
L
Lohrmann on Cybersecurity
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
The Register - Security
The Register - Security
Recorded Future
Recorded Future
Vercel News
Vercel News
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
I
Intezer
The Hacker News
The Hacker News
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
Help Net Security
Help Net Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Scott Helme
Scott Helme
T
Threatpost
爱范儿
爱范儿
N
Netflix TechBlog - Medium
D
Docker
云风的 BLOG
云风的 BLOG
C
Cisco Blogs
K
Kaspersky official blog
H
Help Net Security
S
Secure Thoughts
T
Threat Research - Cisco Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Security @ Cisco Blogs
Cyberwarzone
Cyberwarzone
N
News and Events Feed by Topic
G
Google Developers Blog
Forbes - Security
Forbes - Security
博客园 - 三生石上(FineUI控件)
博客园 - 叶小钗
B
Blog
Google DeepMind News
Google DeepMind News
Recent Announcements
Recent Announcements
Simon Willison's Weblog
Simon Willison's Weblog
S
Securelist
P
Privacy International News Feed
Spread Privacy
Spread Privacy
The Last Watchdog
The Last Watchdog

kefan.me

Two Classes 在冰岛抵抗白昼 用Rust重构了后端 写前端如逆水行舟 鱼王 Coding in Wenyan 去开会成为一个人新生活的标志 Install Clang-Format Without Root Privileges Build a Web App - Part 2/2 Build a Web App - Part 1/2 我说我不来你非要让我来 痛苦还是无聊 从零搭建博客 写前端不是请客吃饭 一些片段 Running Valgrind on macOS Catalina Fetching Github Stats using GraphQL 适合私人沉迷 关于李志 Miller-Rabin素性测试和大素数生成 你好,博客 再见
Odd Ways to Find Odd Numbers
2019-05-28 · via kefan.me

As I was doing one of the problems on Leetcode a few days ago, I was about to write a function that decides an integer’s parity (weather it’s odd or even). It should be an easy problem, anyone with few minutes of coding experiences would be able to write it out easily. Unexpectedly, it took me much more trails than I thought I needed before I got it correct. When I passed the whole problem, I thought over this interesting part and decided to write it down.

(Note: All code written in Java)

A beginner would write something like this:

public boolean is_odd(int x) {
    if (x % 2 == 1) {
        return true;
    }
    else {
        return false;
    }
}

It is simple and pretty easy to understand, but not clean enough. Since the returning value is boolean, and the control statement only takes one line, the function should be simplified as:

public boolean is_odd(int x) {
    return (x % 2 == 1);
}

In fact, this is what I originally had. Soon I realized there are also negative numbers, thus the function becomes:

public boolean is_odd(int x) {
    return (x % 2 != 0);
}

This function satisfies my need, but as I was reviewing my note on linear circuit, the usage of AND gate inspired me and I realized I can also do the same thing with:

public boolean is_odd(int x) {
    return (x & 1) == 1;
}

Since any even number can be written in the form of (2n), when it is transformed into binary, the last digit must be 0 (because 2^0 = 1), on the other hand, if it is an odd number, then this last digit must be 1. When ’&’ is applied, the only situation where the answer would be 1, is when both operand ends with digit 1, and it would mean the input value is an odd number.

Another method involving bit-wise operator is:

public boolean is_Odd(int x) {
    return i >> 1 << 1 != i;
}

Similarly as above, when a number is shifted to the right once and shifted to the left once. It forces the last digit to be zero. If it is still the same number, then the original number does not contains 2^0, which would make it even.

In both cases, the trick is to find if the last digit of the binary number is 1. One used AND gate and one used shifting.

nice.