当前位置:网站首页>Splicing and splitting of integer ints

Splicing and splitting of integer ints

2022-07-07 20:11:00 junxuezheng

One 、int Splicing and splitting of

1. Split

  • int Data types occupy 32 position .
  • Split into high 30 Bit and low 2 position , In fact, it's a 32 The height of a bit 30 Bit and low 2 Bit data is taken out .
  • for example int a =14
  • a Stored hex :0x0000 000E
  • a Stored binary :0000 0000 0000 0000 | 0000 0000 0000 1110
  • a Split height 30 Is it :( Binary system )0000 0000 0000 0000 | 0000 0000 0000 11, The conversion 10 Into the system for 3
  • a Split low 2 Is it :( Binary system )10, Convert to decimal to 2.
     Insert picture description here

2. Splicing

The order of splicing and splitting is reversed ,
Simply put, put the data a The height of 30 Bit shift left 2 position ( Binary system 0000 0000 0000 0000 | 0000 0000 0000 11),
Then add low 2 A data ( Binary system 10)

Two 、c++ demo

#include <iostream>
using namespace std;

int main()
{
    
    int a = 14;
    //  hold int a Split into left 30 position , Right 2 position 
    int a_left = (a & 0xfffffffc) >> 2;
    int a_right = (a & 0xfffffff3);
    //  Merge 
    int a_ori = (a_left << 2) + a_right;


    std::cout << "a_left="<<a_left<<endl;
    std::cout << "a_left=" << a_right << endl;
    std::cout << "a_ori=" << a_ori << endl;
}

Output

a_left=3
a_left=2
a_ori=14
 Insert picture description here

Reference resources :
C++ Operator takes two 32 Digit splicing 64 digit

原网站

版权声明
本文为[junxuezheng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071801455181.html