⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bitmap.cpp

📁 when detect the bit meet, then set th bit in bit map
💻 CPP
字号:
// This function is used to find the SIPia port index when there is only one
// SIPia port reference the IP port object
#include <iostream>

using namespace std;

void
set_bitmap(unsigned short bit_num, unsigned long *bitmap)
{
             // Based on the SIPis port index to set the map
            // setSIPiaPortIdxMap(sipia_port_idx);
            
            // based on the SIPia port index to set the bit map
            // Note: The starting bit is 1. So when sipia port index
            // is 1, the last bit of SIPiaPortIdxMap is 0x1
            *bitmap = (*bitmap|(0x1<<(bit_num-1)));
}

void
print_bitmap(unsigned long bitmap)
{
            int i=0;
            
            for(i=1; i<32;i++)
            {
               printf("%d",i);
            }
            cout<<endl;
            for( i = 0; i<31; i++ )
            {
                if(i<10){
                // From low to high print the bit value
                printf("%d",((bitmap>>i)&(0x1)));
                }else{
                printf(" %d",((bitmap>>i)&(0x1)));
                }

            }
            cout<<endl;
}

unsigned short
get_theOnlybit(unsigned long bitmap)
{
      // it's assumed there is only one bit in the bit map
      // search it from the low to high and one find that
      // bit, return the bit
      unsigned short i=0;
      //printf("get_theOnlybit: bitmap is %d",bitmap);
      
      if(bitmap == 0) return 0;
      for(i=0;  i<32;i++)
      {
       if(((bitmap>>i)&(0x1))==0x1) return (i+1);
      }
      return 0;
}

unsigned short
zero_bitmap(unsigned short bit_num, unsigned long *bitmap)
{
      // this function will set the specified bit to 0;
      *bitmap=(*bitmap&(~(0x1<<(bit_num-1))));
                     
}

int
main()
{

      unsigned long SIPiaPortIdxMap;

      int i;
  
                        
      unsigned short sipia_port_idx =1;
      
      SIPiaPortIdxMap =0;     
      while (sipia_port_idx != 0 )
      {

            cin >> sipia_port_idx;
            cout << "\ninput sipia port idx is "<<sipia_port_idx<<endl;
            
            // Based on the SIPis port index to set the map
            // setSIPiaPortIdxMap(sipia_port_idx);
            
            // based on the SIPia port index to set the bit map
            // Note: The starting bit is 1. So when sipia port index
            // is 1, the last bit of SIPiaPortIdxMap is 0x1
            // SIPiaPortIdxMap = (SIPiaPortIdxMap|(0x1<<(sipia_port_idx-1)));
            printf("Set the bit====\n");                 
            set_bitmap(sipia_port_idx, &SIPiaPortIdxMap);
            print_bitmap(SIPiaPortIdxMap);
            printf("The SIPia Port index is %d\n",get_theOnlybit(SIPiaPortIdxMap));
            

      }
      sipia_port_idx=1;
      while (sipia_port_idx != 0 )
      {

            cin >> sipia_port_idx;
            cout << "\ninput sipia port idx is "<<sipia_port_idx<<endl;
                  
            printf("Clear the bit ===\n");
            
            print_bitmap(SIPiaPortIdxMap);
            zero_bitmap(sipia_port_idx, &SIPiaPortIdxMap);
            print_bitmap(SIPiaPortIdxMap);
      }
            
      return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -