📄 ex9_5.c
字号:
/*
Ex9_5
Set a certain field of an int by a function called-- BIT_SET --
*/
int int_size(test)
unsigned int test;
{
int bitlenth=0;
while ( test!=0 )
{
test /= 2;
bitlenth += 1;
}
return(bitlenth);
}
bit_set(pic,value,n,length)
int *pic; /* point at an int */
int value; /* value to be set in a certain field */
int n; /* the field start at bit_n */
int length; /* length of the field */
{
int set; /* temp var to set the field all in '1' */
set = ~(~0<< length);
*pic = (( set & value )<< n) | ( ~(set << n)& (*pic));
/* above formular*/
/* front part-- set the value in the field
other fields are in'0' */
/* back part -- reset '0' in the field
others of the int reserved */
}
main()
{
int one; /* An int to be used in bit_set */
unsigned int test=~0;
int v,start,l;
printf("---Ans905 ---\n");
printf("Input one int : set_value : start_field : length\n");
scanf("%d %d %d %d",&one,&v,&start,&l);
if ( start+l > int_size(test) )
printf( " The length of your bits is longer than the int_size of this\
machine!\n");
else
bit_set(&one,v,start,l);
printf("=== RESULT === : %d\td %o\to %x\tx\n",one,one,one);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -