找到一个给定数量的使用按位运算的平方根 [英] Finding the square root of a given number using bitwise operations

查看:372
本文介绍了找到一个给定数量的使用按位运算的平方根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个算法使用位运算找到一个定数的平方根?

Is there an algorithm to find the square root of a given number using bitwise operations?

推荐答案

这个著名的一块$的C $Ç魔术的,计算的的平方根有一些非常聪明的位操作。它错误地归功于约翰·卡马克 - 这里有一个更深层次的挖掘到它的起源。也许这就是你问的?

There is this famous piece of code magic that computes inverse square root with a some very clever bit twiddling. It is wrongfully attributed to John Carmack - here's a deeper dig into its origin. Maybe that's what you're asking about?

我不建议使用它,但。在现代的CPU是不能战胜专用超然的说明。你通常的C ++内在的sqrt()很可能会战胜它手了。

I wouldn't suggest using it, though. On modern CPU's it cannot beat dedicated transcendental instructions. Your usual c++ intrinsic sqrt() would probably beat it hands down.

所引用的文章描述了一个通用的推导方法,这种快速的逼近,并明确指出推导的sqrt(x)的类似的方法作为其最终线一门功课的问题。所以,你应该能够直接跟踪其推理和制定一个类似的方法开方(不倒数)。

The cited article describes a general derivation method for such fast approximations, and explicitly states 'Derive a similar method for sqrt(x)' as a homework problem at its final lines. So you should be able to track its reasoning and devise a similar method for sqrt (without the reciprocal) directly.

这篇关于找到一个给定数量的使用按位运算的平方根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆