class10.21

本文深入探讨了二进制位操作的多种实用技巧,包括计算二进制中1的个数、分离奇偶数位并输出、以及比较两个整数二进制表示的位差异。通过具体的C语言代码示例,读者可以学习到如何高效地进行位级数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写一个函数返回参数二进制中 1 的个数

#include<stdio.h>
int count_one_bits(unsigned int value)
{
	int count = 0;
	while (value)
	{
		if (value % 2 == 1)
		{
			count++;
		}
		value = value / 2;
	}
	return count;
}
int main()
{
	printf("%d", count_one_bits(15));
	system("pause");
}

2.获取一个数二进制序列中所有的偶数位和奇数位,
分别输出二进制序列。

#define _CRT_SECURE_NO_WARNINGS 
#include<stdio.h>
#include<stdlib.h>
int main()
{
	int num = 0, i = 0;
	scanf("%d", &num);
	printf("奇数序列为:");
	for (i = 31; i >= 0; i -= 2)  //也是控制32次(每个整型数按32位来算),只是从31位开始移动  
	{
		printf("%d ", (num >> i) & 1);  //第一次向右移动31位和1与得到第一位情况,每次变化2位,即可得到奇数位情况  
	}
	printf("\n");
	printf("偶数序列为:");
	for (i = 30; i >= 0; i -= 2)  //向右移动30位,再与1所得结果是原来数字的第二位,再移28位...  
	{
		printf("%d ", (num >> i) & 1);  
	}
	printf("\n");
	system("pause");
	return 0;
}
  1. 输出一个整数的每一位。
#define  _CRT_SECURE_NO_WARNINGS
void print(int n)
{
	if (n / 10 == 0){
		printf("%d", n);
	}
	else
	{
		print(n/10 );
		printf("%d", n % 10);

	}

	}
int main(){
	int num=0;
	printf("num=");
	scanf("%d", &num);
	printf("\n");
	print(num);
	system("pause");
	return 0;

}

4.编程实现:
两个int(32位)整数m和n的二进制表达中,有多少个位(bit)不同?

#define _CRT_SECURE_NO_WARNINGS 
#include<stdio.h>
#include<stdlib.h>
int Dif_bits(int m, int n)
{
	//异或的特征:相同为0.相异为1
	//所以先异或得到一个值,然后数出这个值中1的个数
	int count = 0;
	int value = m^n;
	while (value != 0)
	{
		if ((value & 1) == 1)
		{
			count++;
		}
		value = value >> 1;
	}
	return count;
}
int main()
{
	int num1, num2;
	scanf("%d %d", &num1, &num2);
	int count = Dif_bits(num1, num2);
	printf("%d", count);
	system("pause");
	return 0;
把我的这个文件改成 vue2的版本 <template> <div class="upload-container"> <div v-if="onShow == 1" class="btn-container"> <input id="fileUpload" type="file" @change="handleFileChange"> <el-button class="btn-file" size="small" type="primary" @click="btnFile" >选择文件</el-button> <el-button v-if="fileName !== '未选择任何文件'" type="primary" size="small" @click="uploadFile" >上传</el-button> </div> <div v-if="onShow == 2" class="btn-container"> <el-progress class="progress-bar" :percentage="progress" :stroke-width="20" striped :striped-flow="isUploadRunning" :duration="10" /> <el-button type="primary" size="small" @click="pauseUpload"> {{ isUploadRunning ? "暂停" : "继续" }} </el-button> </div> <div v-if="onShow == 3" class="btn-container"> <span>上传成功!</span><el-button size="small" @click="onShow = 1">再传一个</el-button> </div> </div> </template> <script setup > import * as tus from 'tus-js-client' import { ref } from 'vue' const isUploadRunning = ref(true) const onShow = ref(1) const fileName = ref('未选择任何文件') const progress = ref() const file = ref() const btnFile = () => { document.getElementById('fileUpload')?.click() } const handleFileChange = (e) => { file.value = null file.value = e.target.files[0] fileName.value = file.value.name onShow.value = 1 } var upload const uploadFile = () => { upload = new tus.Upload(file.value, { // Endpoint is the upload creation URL from your tus server endpoint: 'http://192.168.10.21/File/Upload/', // 这里写自己服务器地址 // Retry delays will enable tus-js-client to automatically retry on errors // retryDelays: [0, 3000, 5000, 10000, 20000],//重发 // Attach additional meta data about the file for the server metadata: { filename: file.value?.name, filetype: file.value?.type }, // Callback for errors which cannot be fixed using retries onError: function(error) { this.$message.error(`上传失败:${error}`) // console.log("Failed because: " + error); }, // Callback for reporting upload progress onProgress: function(bytesUploaded, bytesTotal) { var percentage = ((bytesUploaded / bytesTotal) * 100).toFixed(2) progress.value = Number(percentage) if (progress.value === 100) { onShow.value = 3 } // console.log(bytesUploaded, bytesTotal, percentage + "%"); }, // Callback for once the upload is completed onSuccess: function() { console.log('上传成功') } }) onShow.value = 2 // Check if there are any previous uploads to continue. upload.findPreviousUploads().then(function(previousUploads) { // Found previous uploads so we select the first one. if (previousUploads.length) { upload.resumeFromPreviousUpload(previousUploads[0]) } // Start the upload upload.start() }) } // 暂停上传 function pauseUpload() { isUploadRunning.value = !isUploadRunning.value if (isUploadRunning.value) { upload.start()// 继续 } else { upload.abort()// 暂停 } } </script> <style lang="scss" scoped> .upload-container { width: 300px; } .btn-container { display: flex; position: relative; .progress-bar { width: 200px; } .btn-file { position: absolute; left: 0px; } } </style>
03-12
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值