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

推荐订阅源

C
Cisco Blogs
Cyberwarzone
Cyberwarzone
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
SecWiki News
SecWiki News
Martin Fowler
Martin Fowler
T
Tor Project blog
N
Netflix TechBlog - Medium
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
V
Visual Studio Blog
GbyAI
GbyAI
PCI Perspectives
PCI Perspectives
D
DataBreaches.Net
Jina AI
Jina AI
H
Heimdal Security Blog
云风的 BLOG
云风的 BLOG
P
Privacy International News Feed
A
About on SuperTechFans
J
Java Code Geeks
美团技术团队
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
News | PayPal Newsroom
有赞技术团队
有赞技术团队
MyScale Blog
MyScale Blog
博客园 - 司徒正美
C
Check Point Blog
T
Threat Research - Cisco Blogs
Attack and Defense Labs
Attack and Defense Labs
宝玉的分享
宝玉的分享
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
C
Cyber Attacks, Cyber Crime and Cyber Security
I
Intezer
P
Proofpoint News Feed
Blog — PlanetScale
Blog — PlanetScale
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Vercel News
Vercel News
I
InfoQ
阮一峰的网络日志
阮一峰的网络日志
Cisco Talos Blog
Cisco Talos Blog
W
WeLiveSecurity
Hacker News: Ask HN
Hacker News: Ask HN
Recent Commits to openclaw:main
Recent Commits to openclaw:main
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
D
Docker
博客园 - Franky
Security Archives - TechRepublic
Security Archives - TechRepublic

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.