C Voice: Print odd and even bits of integer binary

topic:

Get all the even and odd bits in an integer binary sequence , and print out the binary sequence respectively

                    

 =========================================================================

                       

Ideas:

general idea:

(1). Input data

                         

(2). Print odd digits :

Use a for loop to generate an even number between 1 and 32 ,

Use the shift operator to  move the even bits , and then the odd bits after shifting ,

At this time , take out the current bit by bit and 1 ,

to print . Newline after printing odd numbers

                     

(3). Print even digits :

Use the for loop loop to generate odd numbers between 1 and 32 ,

Use the shift operator to  move the odd bits , and the shifted bits are even bits ,

At this time , take out the current bit by bit and 1 ,

to print . Newline after printing odd numbers

                


                 

first step:

(1). Input data -- scanf() function

                     

Implementation code:

#include <stdio.h>
int main()
{
	//输入数据:
	int n = 0;
	scanf("%d", &n);


	return 0;
}

Realize the picture:

                 


                 

Step two:

Print odd digits :

             

(1). Use the for loop to generate an even number between 1 and 32 ,

            

(2). Use the shift operator to  move the even bits , and the shifted bits will be odd bits .

At this time , take out the current bit by bit and 1 ,

                

(3) .Print . Newline after printing odd numbers

                     

Implementation code:

#include <stdio.h>
int main()
{
	//输入数据:
	int n = 0;
	scanf("%d", &n);

	//打印 奇数位 :
	printf("奇数位:");
	//使用 for循环 循环产生 1~32 之间的偶数
	int i = 0;
	for (i = 30; i >= 0; i -= 2)
	//循环产生的偶数位:30 28 26 ...
	{
		//使用 移位操作符 和 按位与 来取出当前位 并打印
		printf("%d", (n >> i) & 1);
		//移位后就是奇数位,按位与1 取出最低位
	}

	//换行
	printf("\n");



	return 0;
}

Realize the picture:

                 


                  

third step:

Print even digits :

             

(1). Use the for loop to generate odd numbers between 1 and 32 ,

            

(2). Use the shift operator to  move the odd bits , and the shifted bits will be even bits .

At this time , take out the current bit by bit and 1 ,

              

Implementation code:

#include <stdio.h>
int main()
{
	//输入数据:
	int n = 0;
	scanf("%d", &n);

	//打印 奇数位 :
	printf("奇数位:");
	//使用 for循环 循环产生 1~32 之间的偶数
	int i = 0;
	for (i = 30; i >= 0; i -= 2)
	//循环产生的偶数位:30 28 26 ...
	{
		//使用 移位操作符 和 按位与 来取出当前位 并打印
		printf("%d", (n >> i) & 1);
		//移位后就是奇数位,按位与1 取出当前位
	}

	//换行
	printf("\n");

	//打印 偶数位 :
	printf("偶数位:");
	//使用 for循环 循环产生 1~32 之间的奇数
	for (i = 31; i >= 1; i -= 2)
		//循环产生的奇数位:31 29 27 ...
	{
		//使用 移位操作符 和 按位与 来取出当前位 并打印
		printf("%d", (n >> i) & 1);
		//移位后就是偶数位,按位与1 取出当前位
	}

	return 0;
}

Realize the picture:

                    

Final code and implementation effect

Final code:

#include <stdio.h>
int main()
{
	//输入数据:
	int n = 0;
	scanf("%d", &n);

	//打印 奇数位 :
	printf("奇数位:");
	//使用 for循环 循环产生 1~32 之间的偶数
	int i = 0;
	for (i = 30; i >= 0; i -= 2)
	//循环产生的偶数位:30 28 26 ...
	{
		//使用 移位操作符 和 按位与 来取出当前位 并打印
		printf("%d", (n >> i) & 1);
		//移位后就是奇数位,按位与1 取出当前位
	}

	//换行
	printf("\n");

	//打印 偶数位 :
	printf("偶数位:");
	//使用 for循环 循环产生 1~32 之间的奇数
	for (i = 31; i >= 1; i -= 2)
		//循环产生的奇数位:31 29 27 ...
	{
		//使用 移位操作符 和 按位与 来取出当前位 并打印
		printf("%d", (n >> i) & 1);
		//移位后就是偶数位,按位与1 取出当前位
	}

	return 0;
}

Realize the effect:

Guess you like

Origin blog.csdn.net/weixin_63176266/article/details/131220061