动态转移左边或右边 [英] Dynamically shift left OR right

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

问题描述

我有一个pretty简单的问题。有没有办法来动态逐位向左或向右,例如移根据一个int的迹象。

I have a pretty simple question. Is there a way to dynamically shift bitwise to the left OR to the right, e.g. depending on the sign of an int.

signed int n = 3;
signed int m = -2;

int number1 = 8;
int number2 = 8;

//number1 shift n;
//number2 shift m;

在这种情况下,我想数字1 3比特2比特左移和数字2到右侧。有没有如果别人

In this case I want to shift number1 3 bits to the left and number2 2 bits to the right. Is there a way without if else?

推荐答案

确实算作一个的if-else

int x = (n > 0) ? (number1 << n) : (number1 >> (-n));
int y = (m > 0) ? (number2 << n) : (number2 >> (-m));

这篇关于动态转移左边或右边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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