实施逻辑右移 [英] Implementing a logical shift right

查看:110
本文介绍了实施逻辑右移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在研究nand2tetris项目,并且由于硬件不支持,我想在软件级别实现右移逻辑.

So I'm working on the nand2tetris project, and I want to implement shift right logical on a software level since the hardware doesn't support it.

我知道右移逻辑是二分法.因此,我实现它的第一步是计算在值变为0或为负之前我能够从初始值中减去2的次数.如果数字为负,则类似.

I know shift right logical is a division by two. So my first shot at implementing it would count the number of times I was able to subtract 2 from the initial value before the value became 0 or negative. Similar for if the number was negative.

但是,我发现了一个不起作用的情况.我想右移-27139.移位后的二进制值是19199.应该是19198.因此,我正在寻找一种实现移位的新方法.

But, I've found a scenario where it's not working. I want to shift right -27139. Well the binary value after shifting is 19199. It should be 19198. Thus I'm looking for a new way to implement the shift.

我可以and个值,or个值,addsubtract,这就是我所拥有的全部.

I can and values, or values, add and subtract, and that's about all I have at my disposal.

OSFTW

这是我在Hack实现的汇编语言中的代码:

Here's the code I have in the Hack implementation's assembly language:

//============================================================
// SLR: Shift Logical Right (by 1)
//============================================================

(SLR)

@SLR.1              // Load value
D=M
@SLR.2
M=0                 // Clear variables
@SLR_POSITIVE_LOOP      // If value is positive, go to the positive loop
D;JGT


(SLR_NEGATIVE_LOOP)
@SLR.1              // Add 2 to value, since it's negative
M=M+1
M=M+1
@SLR.1              // If initial value was negative and current value is positive or zero, jump out of loop
D=M
@SLR_NEG
D; JGE
@SLR.2              // If value is negative, add 1 to SLR.2 (we're dividing)
M=M+1
@SLR.1              // If value is less than 0, restart the loop
D=M
@SLR_NEGATIVE_LOOP
D; JLT

(SLR_NEG)
@SLR.2
D=M
D=!D                // Invert the result
D=D+1               // Add 1 to finish converting
@32767              // And it with 0111111111111111 to clear the sign bit
D=D&A
@SLR.2
M=D                 // Set value
@SLR_END                        // Jump to end of loop
0;JMP

(SLR_POSITIVE_LOOP)
@SLR.1              // Subtract 2 from value
M=M-1
M=M-1
@SLR.1              // If initial value was positive and current value is negative or zero, jump out of loop
D=M
@SLR_END
D; JLE
@SLR.2              // If value is still positive, add 1 to SLR.2 (we're dividing)
M=M+1
@SLR.1              // If value is greater than 0, restart the loop
D=M
@SLR_POSITIVE_LOOP
D; JGT


(SLR_END)               // Loop is over. Set value of SLR.1 to that of SLR.2, for return purposes

@SLR.2                                  // Place result in correct place
D=M
@SLR.1
M=D

@SLR.0             // Return to calling function
A = M
0; JMP

推荐答案

逻辑上向左或向右的移位等于将N-n位从N位的一个字复制到另一个.因此:

A logical shift of a number either left or right equals copying N-n bits from one word of N bits to another. Thus:

unsigned int a = 0x1321;
unsigned int b = 0;
unsigned int mask1 = 1;
unsigned int mask2 = 1 << n;  // use repeated addition for left shift...
int i;
for (i = 0; i < N-n; i++) {
    if (a & mask2)
        b|= mask1;
    mask1 += mask1;
    mask2 += mask2;
}

交换mask1和mask2将实现左移(仅按位操作).

Swapping mask1 and mask2 would implement left shift (with bitwise operations only).

这篇关于实施逻辑右移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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