我该怎么做二进制一个bitshift吧? [英] How do i do a bitshift right in binary?

查看:578
本文介绍了我该怎么做二进制一个bitshift吧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这是一个简单的问题,但我不能为我的生活弄清楚如何做一个bitshift二进制。这是在LC3 environemnt正在做。我只需要知道如何通过两到算术鸿沟,并向右移动。我知道要离开是只需添加二进制值本身简单,但我已经试过相对来说bitshift权(从自身减去,指出再扣除等等等等)会非常AP preciated。

Hopefully this is a simple question but I cannot for the life of me figure out how to do a bitshift in binary. This is being done in the LC3 environemnt. I just need to know how to arithmetical divide by two and shift to the right. I know going left is simple by just adding the binary value to itself, but I have tried the opposite for bitshift right(subtracting from itself, NOTing and then subtracting etc etc.) Would be much appreciated.

或者,如果你有更好的方法来x00A0移动到x000A,这也将是非常美妙的。谢谢!

Or if you have a better way to move x00A0 to x000A that would also be fantastic. Thanks!

推荐答案

这是一个较旧的职位,但我遇到了同样的问题,所以我想我会寄我已经找到。

This is an older post, but I ran into the same issue so I figured I would post what I've found.

当你必须做一个位右移你通常(除以2)减半的二进制数但可以在LC-3是一个挑战。这是code我写信给preform一个位右移。

When you have to do a bit-shift to the right you're normally halving the the binary number (divide by 2) but that can be a challenge in the LC-3. This is the code I wrote to preform a bit-shift to the right.

; Bit shift to the right
.ORIG x3000

MAIN
    LD R3, VALUE

    AND R5, R5, #0      ; Reseting our bit counter

    B_RIGHT_LOOP
        ADD R3, R3, #-2     ; Subtract 2 from the value stored in R3
        BRn BR_END      ; Exit the loop as soon as the number in R3 has gone negative 
        ADD R5, R5, #1      ; Add 1 to the bit counter
        BR B_RIGHT_LOOP     ; Start the loop over again 
    BR_END

    ST R5, ANSWER       ; Store the shifted value into the ANSWER variable

    HALT            ; Stop the program

; Variables
VALUE   .FILL x3BBC     ; Value is the number we want to do a bit-shift to the right
ANSWER  .FILL x0000

.END

请与此code中的最左边位B [0]会丢失。此外,如果我们试图转移到右边的数字为负这一code不起作用。所以,如果位[15]设置此code将无法工作。
例如:

Keep in mind that with this code the left most bit B[0] is lost. Also this code doesn't work if the number we are trying to shift to the right is negative. So if bit [15] is set this code won't work. Example:

VALUE .FILL x8000    ; binary value = 1000 0000 0000 0000
                     ; and values higher than x8000
                     ; won't work because their 15th
                     ; bit is set

这至少应该让你去正确的轨道上。

This should at least get you going on the right track.

这篇关于我该怎么做二进制一个bitshift吧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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