简体   繁体   English

如何将2位从一个int复制到另一个?

[英]how can I copy 2 bits from one int to another?

I have two unsigned int numbers: a and b (b is an unsigned int pointer). 我有两个无符号的int数: ab (b是无符号的int指针)。 I want to copy 8th and 9th bit of a to 2nd and 3rd bit of b (all indices are 0 based). 我想的第8和第9位复制a到的第2和第3位b (所有指数0开始)。

This is how I am doing it: 这就是我这样做的方式:

 bool secondBit =  (a & (1 << 8) ) ;
 bool thirdBit =   (a & (1 << 9) ) ;

 if (secondBit) {
     *b |= (1u << 2);
 }
 if (thirdBit) {
     *b |= (1u << 3);

Reminder: b is an unsigned int pointer. 提醒: b是无符号的int指针。

Is there a better way of doing this ? 有没有更好的方法呢?

Clear the relevant bits of *b and set them to the bits you want from a : 清除的相关位*b ,并将其设置为您想从位a

*b = (*b & ~0xC) | ((a & 0x300) >> 6);

// This is the 'not' of 00001100, in other words, 11110011
~0xC;

// This zeros the bits of *b that you do not want (b being a pointer)
*b & ~0xC;   // *b & 11110011

//This clears all of a except the bits that you want
a & 0x300;

// Shift the result into the location that you want to set in *b (bits 2 and 3)   
((a & 0x300) >> 6);

// Now set the bits into *b without changing any other bits in *b
*b = (*b & ~0xC) | ((a & 0x300) >> 6);

Depends on your definition of "better" :) 取决于你对“更好”的定义:)

But, well, there is the std::bitset class in C++. 但是,嗯,C ++中有std::bitset类。 Perhaps it suits your needs by offering a less error-prone interface. 也许它通过提供一个不易出错的界面来满足您的需求。

In the given code, it does not copy the bits - it just ors them. 在给定的代码中,它不会复制这些位 - 它只是它们。 Should it be doing 它应该做什么

*b &= ~0xC0;

first? 第一? Then 然后

*b |= ((a >> 6) & 0xC0);

Here's a more verbose way of creating the result you are looking for and code to test the operation. 这是一个更详细的方法来创建您正在寻找的结果和代码来测试操作。

#include <stdio.h>

void printBits(int n)
{
   int i = 31;
   char bits[32];
   for ( ; i >= 0; --i, n /= 2 )
   {
      bits[i]= n % 2;
   }

   for ( i = 0; i < 32; ++i )
   {
      printf("%d", bits[i]);
      if ( (i+1)%8 == 0 )
      {
         putchar(' ');
      }
   }
}

int foo(int n1, int n2)
{
   // copy 8th and 9th bit of n1 to 2nd and 3rd bit of n2 
   // (all indices are 0 based).

   // Extract the 8th and 9th bits of n1
   int k1 = 0x00000300;
   int r1 = n1 & k1;

   // Clear the 2nd and 3rd bits of n2.
   int k2 = 0xFFFFFFF9;
   int r2 = n2 & k2;

   // Move the 8th and 9th bits of n1 by 6 to the right
   // to put them in 2nd and 3rd places.
   // Construct the result and return.
   return (r1 >> 6) | r2;
}

int main(int argc, char** argv)
{
   int n1 = atoi(argv[1]);
   int n2 = atoi(argv[2]);

   printf("Input n1: ");
   printBits(n1);
   printf("\n");

   printf("Input n2: ");
   printBits(n2);
   printf("\n");

   int n3 = foo(n1, n2);

   printf("Result  : ");
   printBits(n3);
   printf("\n");
}

Sample output: 样本输出:

./test-19 251282 85
Input n1: 00000000 00000011 11010101 10010010
Input n2: 00000000 00000000 00000000 10000000
Result  : 00000000 00000000 00000000 10000100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将位从一个变量复制到另一个变量? - How to copy bits from one variable to another? 如何将内容从一个字符串向量复制到另一个? - How can I copy content from one string vector to another? 用于在任意位置从一个int复制N位到另一个int的算法 - Algorithm for copying N bits at arbitrary position from one int to another 如何将未签名的char缓冲区中的选择复制到另一个变量中并转换为int? - How can I copy a selection from an unsigned char buffer into another variable and convert to int? 如何使用 std::copy 将一张地图复制到另一张地图? - How can I copy one map into another using std::copy? 我可以将TokenPrivileges阵列从一台计算机复制到另一台计算机吗? - Can I copy TokenPrivileges array from one computer to another? 如何将HTML文本框值从一个域复制到另一个域的文本框? - How can I copy HTML textbox values from one domain to another domain's textboxes? c++ - 如何在c/c++中将文件从一个目录复制到另一个目录 - How can I copy a file from one directory to another in c/c++ 如何通过不同的类将一个向量复制到另一个向量 - How can I copy one vector to another through different classes 如何将数据从一个Windows应用程序复制到另一个? - How do I copy data from one windows app to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM