我如何检查一个整数的基本-2再$ P $标准ML psentation? [英] How do I inspect an integer's base-2 representation in Standard ML?

查看:209
本文介绍了我如何检查一个整数的基本-2再$ P $标准ML psentation?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现SML重复平方算法。我希望它是尾递归。

I am trying to implement a repeated squaring algorithm in SML. I want it to be tail recursive.

的目标是乘以所有的x ^ k其中k为2 ^ M和2 ^ m是n的二进制重新presentation 1。

The goal is to multiply all x^k where k is 2^m and 2^m is a 1 in the binary representation of n.

例如,对于x ^ 25,计算X ^ 1 * X ^ 8 * X ^ 16,因为16 + 8 + 1 = 25

E.g, for x^25, calculate x^1 * x^8 * x^16 because 16 + 8 + 1 = 25

我不知道如何重新present一些由它的二进制部件,或者使用位操作来手动检查(因为我可以告诉SML无位运算)。

I have no idea how to represent a number by it's binary parts, or to use bitwise operations to check manually (because from what I can tell SML has no bitwise operations).

我想preFER使用外的开箱SML库,所以没有导入其他库。

I would prefer to use the out-of-the-box SML library, so no importing other libraries.

这是一门功课的问题,因此,如果您可以在没有完全放弃它拿走,这将是巨大的回答。

This is a homework problem, so if you can answer without completely giving it away that would be great.

编辑:我使用的是新泽西州的SML

I'm using SML of New Jersey.

推荐答案

感谢马特,我找到了解决办法。

Thanks to matt, I found the solution.

从SML基础库中使用Word类型,我用:

Using the Word type from the SML basis library, I used:

Word.andb(Word.fromInt(x)中,Word.fromInt(y))为> Word.fromInt(0)

Word.andb(Word.fromInt(x), Word.fromInt(y)) > Word.fromInt(0)

这篇关于我如何检查一个整数的基本-2再$ P $标准ML psentation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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