如何从一个变量复制位到另一个? [英] How to copy bits from one variable to another?

查看:164
本文介绍了如何从一个变量复制位到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有这个 INT 变量 V1

Let's say I have this int variable v1:

1100 1010

和这个变量 INT V2

1001 1110

我需要从 V2 的最后四位复制到 V1 的最后四位,这样的结果是:

I need to copy the last four bits from v2 to the last four bits of v1 so that the result is:

1100 1110
^    ^ last four bits of v2
|
| first four bits of v1

如何我会得到有关在C或C ++这样做呢?我读到位运算几篇文章,但我无法找到明确这个的任何信息。

How would I got about doing this in C or C++? I read a few articles about bitwise operations but I couldn't find any information specifically about this.

推荐答案

位运算是寻找正确的事情。

Bitwise operations were the right things to look for.

v1 = (v1 & ~0xf) | (v2 & 0xf);

有没有一些具体的事情,你没有从文章理解你读?

Is there something specific you didn't understand from the articles you read?

这篇关于如何从一个变量复制位到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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