【笔记】Hacker's Delight - Power-Of-2 Boundaries
Rounding Up/Down to a Multiple of a Known Power of 2(上下取整为2的n次方)
Rounding Down
/// <summary>
/// greatest power of 2 less than or equal to <paramref name="n"/>
/// </summary>
public static uint FloorPowerOf2(uint n)
{n |= (n >> 1);n |= (n >> 2);n |= (n >> 4);n |= (n >> 8);n |= (n >> 16);return n - (n >> 1);
}/// <summary>
/// greatest power of 2 less than or equal to <paramref name="n"/>
/// </summary>
public static ulong FloorPowerOf2(ulong n)
{n |= (n >> 1);n |= (n >> 2);n |= (n >> 4);n |= (n >> 8);n |= (n >> 16);n |= (n >> 32);return n - (n >> 1);
}
Rounding Up
/// <summary>
/// least power of 2 greater than or equal to <paramref name="n"/>
/// </summary>
public static uint CeilingPowerOf2(uint n)
{--n;n |= (n >> 1);n |= (n >> 2);n |= (n >> 4);n |= (n >> 8);n |= (n >> 16);return n + 1;
}/// <summary>
/// least power of 2 greater than or equal to <paramref name="n"/>
/// </summary>
public static ulong CeilingPowerOf2(ulong n)
{--n;n |= (n >> 1);n |= (n >> 2);n |= (n >> 4);n |= (n >> 8);n |= (n >> 16);n |= (n >> 32);return n + 1;
}
Check Power-Of-2 Boundaries(检验是否为2的n次方)
/// <summary>
/// return true if n is power of 2
/// </summary>
public static bool IsPowerOf2(uint n)=> n < 1u ? false : (n & (n - 1u)) == 0u;/// <summary>
/// return true if n is power of 2
/// </summary>
public static bool IsPowerOf2(ulong n)=> n < 1UL false : (n & (n - 1UL)) == 0UL
MSB & LSB
MSB
/// <summary>
/// 0111 1100 0100 1010 1100 1101 1101
/// </summary>
private static readonly uint DeBruijnMSB32 = 0x07C4ACDDU;
private static readonly int[] DeBruijnMSB32Lookup = new int[]
{0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
};
/// <summary>
/// index of left most 1 of an <see cref="uint"/> number using a De Bruijn-like sequence table lookup.
/// </summary>
public static int MostSignificantBit(uint n)
{n |= (n >> 1);n |= (n >> 2);n |= (n >> 4);n |= (n >> 8);n |= (n >> 16);return DeBruijnMSB32Lookup[(n * DeBruijnMSB32) >> 27];
}/// <summary>
/// 0011 1111 0111 1001 1101 0111 0001 1011 0100 1100 1011 0000 1010 1000 1001
/// </summary>
private static readonly ulong DeBruijnMSB64 = 0x03F79D71B4CB0A89UL;
private static readonly int[] DeBruijnMSB64Lookup = new int[]
{0, 47, 1, 56, 48, 27, 2, 60, 57, 49, 41, 37, 28, 16, 3, 61,54, 58, 35, 52, 50, 42, 21, 44, 38, 32, 29, 23, 17, 11, 4, 62,46, 55, 26, 59, 40, 36, 15, 53, 34, 51, 20, 43, 31, 22, 10, 45,25, 39, 14, 33, 19, 30, 9, 24, 13, 18, 8, 12, 7, 6, 5, 63,
};
/// <summary>
/// index of left most 1 of an <see cref="ulong"/> number using a De Bruijn-like sequence table lookup.
/// </summary>
public static int MostSignificantBit(ulong n)
{n |= (n >> 1);n |= (n >> 2);n |= (n >> 4);n |= (n >> 8);n |= (n >> 16);n |= (n >> 32);return DeBruijnMSB64Lookup[(n * DeBruijnMSB64) >> 58];
}
LSB
/// <summary>
/// 0111 0111 1100 1011 0101 0011 0001
/// </summary>
private static readonly uint DeBruijnLSB32 = 0x077CB531U;
private static readonly int[] DeBruijnLSB32Lookup = new int[]
{0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
/// <summary>
/// index of right most 1 of an <see cref="uint"/> number using a De Bruijn-like sequence table lookup.
/// </summary>
public static int LeastSignificantBit(uint n)
{n &= (~n + 1);return DeBruijnLSB32Lookup[(n * DeBruijnLSB32) >> 27];
}/// <summary>
/// 0011 1111 0111 1001 1101 0111 0001 1011 0100 1100 1010 1000 1011 0000 1001
/// </summary>
private static readonly ulong DeBruijnLSB64 = 0x03F79D71B4CA8B09UL;
private static readonly int[] DeBruijnLSB64Lookup = new int[]
{0, 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4,62, 47, 59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5,63, 55, 48, 27, 60, 41, 37, 16, 46, 35, 44, 21, 52, 32, 23, 11,54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6
};
/// <summary>
/// index of right most 1 of an <see cref="ulong"/> number using a De Bruijn-like sequence table lookup.
/// </summary>
public static int LeastSignificantBit(ulong n)
{n &= (~n + 1);return DeBruijnLSB64Lookup[(n * DeBruijnLSB64) >> 58];
}
De Bruijn Sequence
/// <summary>
/// generate De Bruijn sequence of the <see cref="uint"/> number
/// </summary>
public static int[] DeBruijnSequence(uint DebruijnNumber)
{var result = new int[32];for (var i = 0; i < 32; ++i){result[(DebruijnNumber << i) >> 27] = i;}return result;
}/// <summary>
/// generate De Bruijn sequence of the <see cref="ulong"/> number
/// </summary>
public static int[] DeBruijnSequence(ulong DebruijnNumber)
{var result = new int[64];for (var i = 0; i < 64; ++i){result[(DebruijnNumber << i) >> 58] = i;}return result;
}
【笔记】Hacker's Delight - Power-Of-2 Boundaries相关推荐
- Hacker‘s Delight中的Trick
Hacker's Delight 里的各种技巧真有意思. 乘法 Chapter8,32位符号整型乘积的高32位: int mulhs(int u, int v) {unsigned int u0, v ...
- HACKER'S DELIGHT[算法心得]笔记
第一章 概述 略. 第二章 基础知识 2.1 操作最右侧的位元 x & (x - 1) 将最右侧置位的比特位置零, 该表达式可用来判断x是否为2的幂. x | (x + 1) 将最右侧置零 ...
- 【位操作笔记】位反转算法 通过5 * lg(N)次运算完成
位反转算法 通过5 * lgN次运算完成 位反转 算法说明 位反转算法代码 算法来源 算法计算过程 拓展 [参考资料] 位反转 这里的位反转(Bit Reversal),指的是一个数的所有bit位依照 ...
- 老男孩的运维笔记文档-中级部分(运维中级)列表(二)
############################################################ # # USER YYYY-MM-DD – ACTION # ...
- Power BI 数据模型设计及搭建——星型模型雪花模型
前言 之前的笔记提到了<Power BI 数据模型的核心概念>,本文继续深入讨论数据模型的设计架构,同时介绍两种常用的数据模型:星型模型和雪花模型 BI 的数据模型和数仓模型有什么不同? ...
- Bit Twiddling Hacks
前段转自 http://blog.sina.com.cn/s/articlelist_1249713681_14_1.html 作者:很拽的土豆 Bit Twiddling Hacks By Sean ...
- BigDecimal源码分析。
/*** 不可变的.任意精度的有符号十进制数.BigDecimal 由任意精度的整数非标度值 和 32 位的整数标度 (scale) 组成.* 如果为零或正数,则标度是小数点后的位数.如果为负数,则将 ...
- 电子书 http://my.unix-center.net/~Simon_fu/?page_id=724
电子书 原来电子书都存放在163网盘上,近期有网友反映163网盘不给力,广告太多!我决定把电子书慢慢转移到华为115网盘上,如果你也想注册115网盘,请点击Simon的邀请链接,这样你和Simon都将 ...
- java库函数-----Math库函数(每日一更)
java库函数-----Math库函数(每日一更) 目录区 Java中的数学函数的应用:Math.sin,Math.exp. 源码区 /** Copyright (c) 1994, 2013, Ora ...
最新文章
- 【VS开发】【DSP开发】WinDriver简介(或介绍)
- angular,vue,react的基本语法—双向数据绑定、条件渲染、列表渲染、angular小案例...
- C#.Net工作笔记010---c#中的静态扩展方法_可动态给string等_添加共通方法好用
- SQL Server数据库安装和使用
- mysql续型_mysql续集(查询部分)
- Unity3D基础9:获取鼠标键盘输入
- Python中的闭包与参数引用域
- eclipse的自动检查语法错误功能让我有点烦,能不能关掉,或者,只是10秒检查一次。。...
- css3 切换贞动画的效果,仿gif效果
- WORD中输入已知编码的特殊字符
- win7系统升级到ie11浏览器(常见错误,升级经验)
- mac如何配置环境变量
- VC “变速齿轮”再研究
- java转大写的方法_Java字母大小写转换的方法
- 汽车照明全国产化电子元件推荐方案
- 云原生之Kubernetes:24、污点和容忍度详解
- STM32 之十 供电系统及内部参照电压(VREFINT)使用及改善ADC参考电压,内部参照电压的具体方法,只有在STM32F0x芯片的参考手册中才能找到,其他MCU的参考手册都是很简单的说明
- 使用dot.tk和Sina SAE服务免费搭建自己的网站
- VIVADO如何打开.ila波形文件
- Windows Server 2016 Datacenter中安装URL Rewrite 2.0 URL模块报错
热门文章
- latex 花体_latex最后一课
- 编写Linux网络设备驱动(上)
- F280049C FLASH编程报错Break at address “0x3fbd92“ with no debug information available解决方案
- 软件实施经理岗位职责
- AcWing一排奶牛
- [LeetCode](面试题 08.11)硬币
- 电脑C盘清理方法总结
- 本题要求根据某城市普通出租车收费标准编写程序进行车费计算。
- wak切割功能和cut的区别
- ABAP ALV 删除按钮标准写法