密码学中比特循环移位实现( python代码与Java代码)

在密码学中经常用到循环移位,现在自己做一个总结,并给出如下代码:
参数说明
value:数值
shiftBits:移动位数
bits:比特总长度
python代码
   # bit位循环左移
   def life_round(value, shiftBits, bits):
   	t1 = (value >> (bits - shiftBits)) ^ (value << shiftBits)
   	t2 = ((2 ** bits) - 1)
   	return t1 & t2
   # bit位循环右移
   def right_round(value, shiftBits, bits):
   	t1 = (value << (bits - shiftBits)) ^ (value >> shiftBits)
   	t2 = ((2 ** bits) - 1)
   	return t1 & t2
Java代码
	   // bit位循环左移
	    public static int life_round(int value, int shiftBits, int bits) {
	        int t1 = (value >> (bits - shiftBits)) ^ (value << shiftBits);
	        int t2 = (Math.pow(2, bits) - 1);
	        return (t1 & t2);
	    }
	       // bit位循环右移
	    public static int right_round(int value, int shiftBits, int bits) {
	        int t1 = (value << (bits - shiftBits)) ^ (value>> shiftBits);
	        int t2 = (Math.pow(2, bits) - 1);
	        return (t1 & t2);
	    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值