java - Check Leap Year - without using division operation
问题描述
How to check if the given year is a leap year, without using division operation or some library method in Java. Is it possible to check this using a bitwise operator?
PS: validation not necessary for centuries
解决方案
以下是一些不使用模函数来识别闰年的方法。
首先,让我们假设(或测试)年份在范围内1901 - 2099
。
以二进制数表示的闰年将具有
00
最后两位数字。所以:这是闰年
if year & (not 4) == 0
如果您有可将实数截断为整数的函数,则此方法有效:
x = trunc(year / 4)
这是闰年
if x * 4 == year
如果您有移位(不是循环移位)运算符,我确信 Verilog 有:
x = year >> 2
这是闰年
if (x << 2) == year
如果关于范围的假设1901 - 2099
是错误的,那么您将需要一些额外的逻辑来消除1900, 1800, 1700 and 2100, 2200, 2300 and so on
.
推荐阅读
- python - 如何在两个带有嵌套字符串表示的列表的熊猫数据帧之间进行逐行操作
- matrix - 从平面方程计算变换矩阵
- swift - 如何以编程方式在 Xcode 中的视图控制器之间移动用户?
- python - 如何为泰坦尼克号数据填充年龄特征中的 nan 值?
- wordpress - 产品库和灯箱在 woocommerce 单页中不起作用
- c# - 在 MemoryStream 的 C# .net 核心项目中无法正确生成 PDF 文件
- ios - 在物理设备上运行 ios 应用程序黑屏时出错
- javascript - 新换行问题
- javascript - 切换一个使用同一类的 div
- android - Android:TextInputEditText 的底部总是滚动到软键盘的顶部