반응형
*1
int func(unsigned int bits){
unsigned int i;
unsigned int mask=1<<31; //network개념
unsigned int total=0;
for(i=0;i<32;i++,bits<<=1){
if((bits&mask)==mask)
total++;
}
return total%2?1:0;
}
*2
unsigned int num, i, bit[10], count=0;
printf("Enter an interger : ");
scanf("%d",&num);
for(i=0;num;i++){
bit[i]=num%2;
num/=2;
if(bit[i])
count++;
}
printf("The total number of is in the bit representation is %s",count%2?"odd":"even");
*3
bit ~ not operator
for(i=0;i<15;i++,bits<<=1){
if(!((bits&mask)==mask))
reverse++;
reverse<<=1;
}
8.1.
Write a program that takes the integer value and prints “odd” if the total number of 1s in the bit representation is odd, and prints “even” if the number of 1s is even.
Use a bitwise operation (Shift and AND)
#include<stdio.h>
int main()
{
unsigned int num, count=0;
printf("Enter an interger : ");
scanf("%u",&num);
while(num){
if(num%2&1)
count++;
num=num>>1;
}
printf("The total number of is in the bit representation is %s",count%2?"odd":"even");
return 0;
}
8.2.
Write function power2 that takes two integer arguments 𝑛𝑢𝑚𝑏𝑒𝑟 and 𝑝𝑜𝑤 and calculates 𝑛𝑢𝑚𝑏𝑒𝑟 ∗ 2 ^𝑝𝑜𝑤.
Use the shift operator to calculate the result.
Print the values as integers and as bits.
#include<stdio.h>
void display(unsigned int bits);
int cal(unsigned int num, unsigned int pow);
int main()
{
unsigned int num, pow, mask=1<<31, i, result;
printf("Enter two integers :");
scanf("%u %u",&num, &pow);
printf("number :\n\t%2u = ",num);
display(num);
printf("\n\npow :\n\t%2u = ",pow);
display(pow);
printf("\n\n%2u * 2^%u = %2d\n",num, pow, result=cal(num, pow));
printf("%10u = ",result);
display(result);
return 0;
}
void display(unsigned int bits){
unsigned int i;
unsigned int mask=1<<15;
for(i=0;i<16;i++,bits<<=1){
printf("%d",(bits&mask)==mask?1:0);
if(i==7)
printf(" ");
}
}
int cal(unsigned int num, unsigned int pow){
return num<<pow;
}
8.3.
Write a program that reverses the order of the bits in an unsigned integer value.
The program should input the value from the user and call function “reversBits” to print the bits in reverse order.
Print the value in bits both before and after the bits are reversed.
#include<stdio.h>
void display(unsigned int bits);
int reverseBits(unsigned int bits);
int main()
{
unsigned int num, reverse;
printf("Enter an unsigned integer : ");
scanf("%u",&num);
printf("\nBefore bits are reversed :\n");
printf("%10u = ",num);
display(num);
printf("\n\nAfter bits are reversed :\n");
printf("%10u = ",reverse=reverseBits(num));
display(reverse);
return 0;
}
void display(unsigned int bits){
unsigned int i;
unsigned int mask=1<<15;
for(i=0;i<16;i++,bits<<=1){
printf("%d",(bits&mask)==mask?1:0);
if(i==7)
printf(" ");
}
}
int reverseBits(unsigned int bits){
unsigned int reverse=0;
unsigned int i;
unsigned int mask=1;
for(i=0;i<15;i++,mask<<=1,reverse<<=1){
if(((bits&mask)==mask))
reverse++;
}
return reverse;
}
반응형
'학부공부 > C_Basic' 카테고리의 다른 글
10. File[C Basic] (0) | 2021.11.07 |
---|---|
9. Struct[C Basic] (0) | 2021.11.07 |
7. String[C Basic] (0) | 2021.11.07 |
6. Pointer[C Basic] (0) | 2021.11.07 |
5. Array and Functions[C Basic] (0) | 2021.11.07 |
댓글