Implementing x / 6 Using Only Bit Manipulations
This is an interesting question from one of the lab assignments in Introduction to Computer Systems, fall 2018 at Peking University.
Problem Description
Given a 32-bit integer \(x\)(in two's complement), implement a C function that returns \(\frac{x}{6}\) using ONLY bit manipulations(operators like ~ ! | ^ & << >> +
). Your function should behave exactly as the C expression x/6
.
Hint: You can use the following formula(Formula 1)
\]
Inspiration
Since division is very slow using hardware, compilers often use optimizations to speed up division. For example, gcc
will replace x/6
with x*171/1024
when x is relatively small, and implement x*171/1024
with shift left and shift right instructions. However, our function must cover all 32-bit two's complement integers, which means some other techniques are needed to make such replacement possible.
Resolution
We can change Formula 1 into the following form:
\]
Thus we can calculate this(Formula 2)
\]
Which can be implmented using a combination of shift-right and add operations(note that you must program carefully to avoid overflows). However, errors occur since expressions like x>>y
return \(\lfloor x/2^y \rfloor\). We can counter the error by this(Formula 3)
\]
Since errors introduced by shift-rights will only cause \(p\) to be smaller than \(\frac{x}{6}\), we can deduce that \(x-6p > 0\). You can then approximate an upper bound of \(x-6p\), which depends on your implementation of Formula 2.
Suppose that \(x-6p < M\)(where M is small), then we can approximate \(\frac{1}{6}\) in Formula 3 using some \(X \approx \frac{1}{6}\) while keeping the equation true
\]
Choose a proper \(X = a/2^b\), and we are done!
/*
* divSix - calculate x / 6 without using /
* Example: divSix(6) = 1,
* divSix(2147483647) = 357913941,
* Legal ops: ~ ! | ^ & << >> +
* Max ops: 40
* Rating: 4
*/
int divSix(int x) {
int p;
int q,y,t;
x=x+(x>>31&5);
p=x>>3;
p=p+(p>>2);
p=p+(p>>4);
p=p+(p>>8);
p=p+(p>>16);
q=~p+1;
t=x+(q<<1)+(q<<2);
t=t+(t<<1)+(t<<3);
return p+(t>>6);
}
Implementing x / 6 Using Only Bit Manipulations的更多相关文章
- java.lang.IncompatibleClassChangeError: Implementing class的解决办法,折腾了一天总算解决了
一,问题产生背景 git更新代码重启服务器后,问题就莫名奇妙的产生了,一看报错信息,基本看不懂,然后上百度去查,基本都是说jar包冲突,于是把矛头指向maven 二,问题的解决过程 既然确定了是mav ...
- Implementing Navigation with UINavigationController
Implementing Navigation with UINavigationController Problem You would like to allow your users to mo ...
- Implementing SQL Server Row and Cell Level Security
Problem I have SQL Server databases with top secret, secret and unclassified data. How can we estab ...
- ios警告:Category is implementing a method which will also be implemented by its primary class 引发的相关处理
今天在处理项目中相关警告的时候发现了很多问题,包括各种第三方库中的警告,以及各种乱七八糟的问题 先说说标题中的问题 Category is implementing a method which ...
- Hadoop on Mac with IntelliJ IDEA - 3 解决MRUnit - No applicable class implementing Serialization问题
本文讲述在IntelliJ IDEA中使用MRUnit 1.0.0测试Mapper派生类时因MapDriver.withInput(final K1 key, final V1 val)的key参数被 ...
- Implementing the skip list data structure in java --reference
reference:http://www.mathcs.emory.edu/~cheung/Courses/323/Syllabus/Map/skip-list-impl.html The link ...
- The JSR-133 Cookbook for Compiler Writers(an unofficial guide to implementing the new JMM)
The JSR-133 Cookbook for Compiler Writers by Doug Lea, with help from members of the JMM mailing lis ...
- RH253读书笔记(6)-Lab 6 Implementing Web(HTTP) Services
Lab 6 Implementing Web(HTTP) Services Goal: To implement a Web(HTTP) server with a virtual host and ...
- Implementing HTTPS Everywhere in ASP.Net MVC application.
Implementing HTTPS Everywhere in ASP.Net MVC application. HTTPS everywhere is a common theme of the ...
随机推荐
- 1.java内存区域与内存溢出异常
1.java运行时数据区如图所示: 2.每个区域的功能 (1)程序计数器(寄存器) 当前线程所执行的字节码的行号指示器 为了线程切换后能够恢复到正确的执行位置,因此每个线程拥有自己独立的程序计数器 如 ...
- java学习——equals()和==的比较
equals 方法是 java.lang.Object 类的方法. 下面从两个方面来说明equals()和==的差别:(1)对于字符串变量来说,使用“==”和“equals()”方法比较字符串时,其比 ...
- C# 实现java中 wiat/notify机制
最近在学习java,看到wiat/notify机制实现线程通信,由于平时工作用的C#,赶紧用C#方式实现一个demo. Java 代码: import java.util.ArrayList; imp ...
- 【Hadoop】大数据时代,我们为什么使用hadoop
博客已转移,请借一步说话.http://www.daniubiji.cn/archives/538 我们先来看看大数据时代, 什么叫大数据,“大”,说的并不仅是数据的“多”!不能用数据到了多少TB , ...
- 【BZOJ】2406 矩阵
[算法]二分+有源汇上下界可行流 [题解]上下界 题解参考:[BZOJ2406]矩阵(二分+有源汇有上下界的可行流) #include<cstdio> #include<algori ...
- HDU 1166 敌兵布阵 (树状数组 单点修改+区间查询)
题目链接 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和T ...
- 爬虫实战--利用Scrapy爬取知乎用户信息
思路: 主要逻辑图:
- 黑色的cms商城网站后台管理模板——后台
链接:http://pan.baidu.com/s/1hst6Lbm 密码:9ad7
- ISG2018 web题Writeup
0x01.命令注入 这题可以使用burpsuite扫出来,但是可能需要测一下. 得知payload为:i%7cecho%20gzavvlsv9c%20q9szmriaiy%7c%7ca%20%23'% ...
- docker安装总结 linux红帽系列
由于Docker限制分为两个版本CE和EE,所以之前yum里面的docker是老版本,需要先进行卸载,现在的包名叫做docker-ce yum remove docker docker-common ...