#include <stdio.h>
#include <stdlib.h>
int main()
{
char s[20];
gets(s);
for(int i=0;s[i]!='\0';i++)
{
if(s[i] >= 'A' && s[i] <= 'Z')
s[i] += 32;
}
puts(s);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
char s[20];
gets(s);
for(int i=0;s[i]!='\0';i++)
{
if((s[i]>='a'&&s[i]<='m') || (s[i]>='A'&&s[i]<='M'))
s[i]+=13;
else if((s[i]>='n'&&s[i]<='z') || (s[i]>='N'&&s[i]<='Z'))
s[i]-=13;
}
puts(s);
return 0;
}
这里题目中提示不要依赖于自己机器上的整型值长度,所以函数中的循环次数是用一个整型数值移位来控制的,而不是直接用字面值32来控制。
#include <stdio.h>
#include <stdlib.h>
unsigned int reverse_bits(unsigned int value)
{
unsigned int res,i;
for(i=1;i!=0;i<<=1)
{
res <<= 1;
if(value & 1)
res |= 1;
value >>= 1;
}
return res;
}
int main()
{
unsigned int a=25;
printf("%u",reverse_bits(a));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
void set_bit(char bit_array[],unsigned bit_number);
void clear_bit(char bit_array[],unsigned bit_number);
void assign_bit(char bit_array[],unsigned bit_number,int value);
int test_bit(char bit_array[],unsigned bit_number);
int character_offset(unsigned bit_number);
int bit_offset(unsigned bit_number);
int main()
{
char s[]={0xAA};
for(int i=0;i<8;i++)
printf("%d\n",test_bit(s,i));
return 0;
}
int character_offset(unsigned bit_number)
{
return bit_number / CHAR_BIT;
}
int bit_offset(unsigned bit_number)
{
return bit_number % CHAR_BIT;
}
void set_bit(char bit_array[],unsigned bit_number)
{
bit_array[character_offset(bit_number)] |= 1<<bit_offset(bit_number);
}
void clear_bit(char bit_array[],unsigned bit_number)
{
bit_array[character_offset(bit_number)] &= ~(1<<bit_offset(bit_number));
}
void assign_bit(char bit_array[],unsigned bit_number,int value)
{
if(value)
set_bit(bit_array,bit_number);
else
clear_bit(bit_array,bit_number);
}
int test_bit(char bit_array[],unsigned bit_number)
{
char ch = bit_array[character_offset(bit_number)]>>bit_offset(bit_number);
if(ch & 1)
return 1;
else
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int store_bit_field(int original_value,int value_to_store,unsigned starting_bit,unsigned ending_bit)
{
unsigned int mask=0,i=1;
for(int j=ending_bit;j<=starting_bit;j++)//创建掩码,需要改变的位置1
{
mask |= i<<j;
}
//printf("%#x\n",mask);//打印掩码
original_value &= ~mask;//掩码取反和原始值按位与,将需要改变的位置0
value_to_store <<= ending_bit;
value_to_store &= mask;//只保留需要储存的值
original_value |= value_to_store;
return original_value;
}
int main()
{
int original_value,value_to_store,res;
unsigned starting_bit,ending_bit;
original_value=0xffff;
//printf("%#x\n",original_value);
value_to_store=0x123;
starting_bit=13;
ending_bit=9;
res=store_bit_field(original_value,value_to_store,starting_bit,ending_bit);
printf("%#x",res);
return 0;
}