📄 64bit_operator.c
字号:
/*--------------------DESCPRIPTION------------------------------------
this file is uesed to test 64 bits operation like << ,>> , = 1,= 0.
In windows,the byte is small end mode,
so
typedef struct
{
UI16_T low;
UI16_T high;
}UI32_ST;
but most embeded device is large end mode,
if you use it in large mode that you should chang:
typedef struct
{
UI16_T high
UI16_T low;
}UI32_ST;
---------------------END--------------------------------------*/
#include <stdio.h>
#define ONU_MAX_UNI 8
#define xprintf printf
typedef unsigned short UI16_T;
typedef unsigned long UI32_T;
typedef unsigned char UI8_T;
typedef struct
{
UI16_T low;
UI16_T high;
}UI32_ST;
typedef struct
{
UI16_T low;
UI16_T mid_low;
UI16_T mid_high;
UI16_T high;
}UI64_ST;
typedef union
{
UI32_ST data;
UI32_T value;
}UI32_UNION;
void SWAP_WORD(UI32_ST *value)
{
UI16_T temp_value;
temp_value = value->low;
value->high =value->low;
value->high = temp_value;
}
UI16_T RIGHT_DWORD_ONE_BIT(UI16_T *_ptr_bitmap_)
{
UI32_UNION temp;
int right_i;
UI16_T temp_data = 0;
temp.value = 0;
for(right_i = 4;right_i>=0;right_i--)
{
//xprintf("\n\r_ptr_bitmap_[%d] = %x",right_i,_ptr_bitmap_[right_i]);
temp.data.high= _ptr_bitmap_[right_i];
//xprintf("\n\r temp.value =%x",temp.value);
temp.value >>= 1;
//xprintf("\n\r temp_data =%x ;_ptr_bitmap_[%d] = %d,temp.value =%x",temp.data.low,right_i,_ptr_bitmap_[right_i],temp.value);
_ptr_bitmap_[right_i] = (temp.data.high | temp_data);
//xprintf("\n\r _ptr_bitmap_[right_i]=%x",_ptr_bitmap_[right_i]);
temp_data = temp.data.low;
temp.value = 0;
}
return temp_data;
}
void LEFT_DWORD_ONE_BIT(UI16_T *_ptr_bitmap_)
{
UI32_UNION temp;
int left_i;
UI16_T temp_data = 0;
temp.value = 0;
for(left_i = 0;left_i<4;left_i++)
{
//xprintf("\n\r_ptr_bitmap_[%d] = %x",left_i,_ptr_bitmap_[left_i]);
temp.data.low= _ptr_bitmap_[left_i];
//xprintf("\n\r temp.value =%x",temp.value);
temp.value <<= 1;
//xprintf("\n\r temp_data =%x ;_ptr_bitmap_[%d] = %d,temp.value =%x",temp.data.high,left_i,_ptr_bitmap_[left_i],temp.value);
_ptr_bitmap_[left_i] = (temp.data.low | temp_data);
temp_data = temp.data.high;
temp.value = 0;
}
}
void SET_DWORD_BIT(UI64_ST *_ptr_bitmap_,UI16_T bit_num)
{
int i;
UI64_ST temp;
temp.high = 0;
temp.mid_high = 0;
temp.mid_low = 0;
temp.low = 0x01;
for(i = 0;i<bit_num ; i++)
LEFT_DWORD_ONE_BIT((UI16_T *)&temp);
_ptr_bitmap_->low |= temp.low;
_ptr_bitmap_->mid_high |= temp.mid_high;
_ptr_bitmap_->mid_low |= temp.mid_low;
_ptr_bitmap_->high |= temp.high;
}
void CLEAR_DWORD_BIT(UI64_ST *_ptr_bitmap_,UI16_T bit_num)
{
int i;
UI64_ST temp;
temp.high = 0;
temp.mid_high = 0;
temp.mid_low = 0;
temp.low = 0x01;
for(i = 0;i<bit_num ; i++)
LEFT_DWORD_ONE_BIT((UI16_T *)&temp);
temp.low = (~temp.low);
temp.mid_high= (~temp.mid_high);
temp.mid_low = (~temp.mid_low);
temp.high = (~temp.high);
_ptr_bitmap_->low &= temp.low;
_ptr_bitmap_->mid_high &= temp.mid_high;
_ptr_bitmap_->mid_low &= temp.mid_low;
_ptr_bitmap_->high &= temp.high;
}
UI16_T GET_DWORD_BIT(UI64_ST *_ptr_bitmap_,UI16_T bit_num)
{
UI16_T temp_data = 0;
UI64_ST temp;
int i;
temp.high = _ptr_bitmap_->high;
temp.mid_high = _ptr_bitmap_->mid_high;
temp.mid_low = _ptr_bitmap_->mid_low;
temp.low = _ptr_bitmap_->low;
//xprintf("\n\r temp.high = %x ; temp.low =%x",temp.high,temp.low );
for(i = 0;i <= bit_num ; i++)
temp_data = RIGHT_DWORD_ONE_BIT((UI16_T *)&temp);
//printf("\n\rGET_DWORD_BIT temp_data = %d",temp_data);
if(temp_data !=0)
temp_data = 1;
return temp_data;
}
int ST_UTL_UNI_Portlist_Get_FirstPort(UI8_T *list, int clear)
{
int i;
for(i = 0;i<(ONU_MAX_UNI*8);i++)
{
if(0x01 != GET_DWORD_BIT((UI64_ST *)list,i))
{
continue;
}
if(clear)
{
CLEAR_DWORD_BIT((UI64_ST *)list,i);
}
break;
}
if((ONU_MAX_UNI*8) <= i)
{
return -1;
}
return (i + 1);
}
int ST_UTL_UNI_Portmap_Get_FirstPort(UI8_T *buff, int clear)
{
return ST_UTL_UNI_Portlist_Get_FirstPort(buff, clear);
}
void test_64bit( void )
{
//UI16_T temp[4]={0,0,0,0};
UI64_ST temp,*ptr;
UI32_T temp_a = 0x0706;
UI32_UNION temp_un;
UI16_T state;
int i;
//temp[1]= 0x80;
temp.high=0x01;
temp.mid_high =0x01;
temp.mid_low = 0x01;
temp.low=0x8007;
temp_un.value = 0x00028007;
ptr = &temp;
state = GET_DWORD_BIT(&temp,0);
for(i = 0;i<4;i++)
{
xprintf("\n\rtemp[%d] = %x ",i,((UI16_T *)&temp)[i]);
}
xprintf("\n\r state = %d",state);
SET_DWORD_BIT(&temp,0);
for(i = 0;i<4;i++)
{
xprintf("\n\rtemp[%d] = %x ",i,((UI16_T *)&temp)[i]);
}
state = GET_DWORD_BIT(&temp,0);
xprintf("\n\r temp[34] = %d ",state);
for(i = 0;i<4;i++)
{
xprintf("\n\rtemp[%d] = %x ",i,((UI16_T *)&temp)[i]);
}
CLEAR_DWORD_BIT(&temp,0);
xprintf("\n\rtemp[34] = %d ",GET_DWORD_BIT(&temp,0));
for(i = 0;i<4;i++)
{
xprintf("\n\rtemp[%d] = %x ",i,((UI16_T *)&temp)[i]);
}
for(i=0;(ST_UTL_UNI_Portmap_Get_FirstPort((UI8_T *)ptr,1))>0;i++)
;
xprintf("\n\r %d\n",i);
xprintf("\n\r %x\n",temp_a >> 1);
xprintf("\n\r temp_un.data[0] = %x; temp_un.data[1] = %x",temp_un.data.high,temp_un.data.low);
}
main()
{
test_64bit();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -