码迷,mamicode.com
首页 > 其他好文 > 详细

第15章 位运算

时间:2019-05-25 21:32:34      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:整数   sam   inpu   har   int   提取   bsp   printf   font   

Description

已知键盘输入的单字节的数据中,存有两项信息:第2位存有性别(0代表male,1代表female),第3、4、5位存有班级(000代表1班,001代表2班,010代表3班,...,111代表8班)。请编程,从输入的数据中提取这两项信息并输出。说明:每个字节的最右边是第0位

Input

输入一个单字节的数据

Output

输出两项信息

Sample Input

12

Sample Output

2,female
 
 

#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,b;
scanf("%d",&a);
b=a;
b=b>>3;
b=b%8;
printf("%d",b+1);
a=a&4;
if(a==0)
{
printf(",male");
}
if(a==4)
{
printf(",female");
}
return 0;
}

 

Description

键盘输入一个正整数,输出其二进制数据
本题必须用位运算的方法做, 否则不得分。

Input

输入一个十进制正整数

Output

输出二进制数(整数在内存中占多少位,则输出多少位)

Sample Input

100

Sample Output

00000000000000000000000001100100 
 
 

#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,c[32];
scanf("%d",&a);
int i,b;
b=1;
for(i=0;i<32;i++)
{
if(a&b)
{
c[i]=1;
}
else
{
c[i]=0;
}
b=b<<1;
}
for(i=31;i>=0;i--)
{
printf("%d",c[i]);
}
return 0;
}

Description

键盘输入一个整数(正、负、0均可),求其补码或原码(后面输入的数是1则求原码,若是2则求补码)

Input

 

任意int型数据,然后再输入一个整数决定求原码还是补码(1----原码,2---补码)

Output

原码或补码

Sample Input

100 1

Sample Output

00000000000000000000000001100100
 

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,n;
char k;
scanf("%d%c",&n,&k);
while(k!=‘1‘&&k!=‘2‘)
{
k=getchar();
}
if(k==‘1‘&&n<0)
{
n--;
n^=~0;
n|=1<<sizeof(int)*8-1;
}
for(i=sizeof(int)*8-1;i>=0;i--)
{
printf("%d",n>>i&1);
}
printf("\n")
return 0;
}

第15章 位运算

标签:整数   sam   inpu   har   int   提取   bsp   printf   font   

原文地址:https://www.cnblogs.com/deer-king/p/10923896.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!