二进制右移,只给出加法 [英] Binary Right Shift, Given Only Addition

查看:269
本文介绍了二进制右移,只给出加法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个项目,我正在读的存储位置,需要输出的ASCII十六进制值。

I am working on a project where I am reading memory locations and need to output their hex value in ASCII.

语言给了我一个16位字长的,所以我有必要划分在同一时间抢四位以转换为十六进制。不幸的是,这种语言仅提供与,或,不和添加数学/逻辑功能。

The language gives me a 16 bit word length, so I have a need to divide to grab a nibble at a time to convert to hex. Unfortunately, the language only offers and, or, not, and add for mathematical/logical functions.

我已经想通我可以创建左移和测试负标志预期的效果后,转移到1添加到末尾,但我盘算必须有这样做的更好的方法。

I've figured I can create the desired effect by left shifting and testing for a negative flag to add a 1 to the end after shifting, but I'm figuring there has to be a better method for doing this.

任何有识之士将AP preciated。

Any insight would be appreciated.

推荐答案

所以原来的方法我用的工作。我也想出了另一个柜面有谁再有这个问题。

So the original method I used worked. I also came up with another, incase anyone ever has this problem again.

我建立了以时计算4位,并根据该评估中的号码,一些C风格伪$ C $子程序c那么它看起来是这样的:

I built a subroutine that evaluates 4 bits at a time and creates a number based on the evaluation, for some C style pseudo code it looks like this:

16bitSignedInt bin; //binary being analyzed
int value; //number being built

for (int i = 0; i < 4; i++) // while 0-3, for each nibble of the 16 bits
{
   if (bin.bit15 = 1)
      value += 8; // dominate bit in nibble

   bin <<= 1; // left shift 1

   if (bin.bit15 = 1)
      value += 4; // 2nd bit in nibble

   bin <<= 1; // left shift 1

   if (bin.bit15 = 1)
      value += 2; // 3rd bit in nibble

   bin <<= 1; // left shift 1

   if (bin.bit15 = 1)
      value += 1; // last bit in nibble

   bin <<= 1; // left shift 1

   //do work with value
}

原油,但有效。

这篇关于二进制右移,只给出加法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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