0

0

切换一个数字的第一个和最后一个位

PHPz

PHPz

发布时间:2023-08-27 17:57:07

|

1148人浏览过

|

来源于tutorialspoint

转载

切换一个数字的第一个和最后一个位

The following article provides an in depth explanation of the method used to modify a number by toggling its first and last bit using bitwise operators. A bitwise operator is an operator that can be used to manipulate individual bits in binary numbers or bit patterns.

Problem Statement

For a given number n, modify the number such that the first and the last bit of the binary expansion of the new number are flipped i.e. if the original bit is 1 then the flipped bit should be 0 and vice versa. All the bits between the first and the last bit should be left unchanged.

Examples

Input: 13
Output: 4

Explanation

的中文翻译为:

解释

The binary expansion of 13 is 1101.

在切换第一个和最后一个位之后,扩展变为0100,等于4。

因此,输出结果为4。

Input: 27
Output: 10

Explanation

的中文翻译为:

解释

The binary expansion of 27 is 11011.

在切换第一个和最后一个位之后,扩展变为01010,等于10。

Hence the output is 10.

Input: 113
Output: 48

Explanation

的中文翻译为:

解释

The binary expansion of 113 is 1110001.

On toggling the first and the last bit, the expansion becomes 0110000 which is equal to 48.

Hence the output is 48.

Solution Approach

This approach makes use of the bitwise XOR and left shift operator. If the corresponding bit of both operands is different, the bitwise XOR operator evaluates to 1; otherwise, it evaluates to 0. We'll employ the bitwise XOR operator's ability to toggle a bit. For instance, if the first bit of the number, n, is 1, then n ^ 1 will cause the number's first bit to be 0. In addition, if the number's first bit is set to 0, the operation n ^ 1 will change it to 1.

要翻转数字n的第一个位,我们计算n ^ 1。它执行一个异或操作,将n的最低有效位或第一个位与1进行反转。

为了翻转最后一位,我们生成一个只有最后一位被设置的数字k。最后一位的位置r等于log2(n)。这是因为在n的二进制展开中使用了log2(n)位。

The following steps are performed to implement this approach −

  • If n = 1, display 0 and return.

  • Toggle the first bit of the number by taking an XOR of the n with 1.

  • 通过将n与1th位。

  • Display the answer.

干跑

首先让我们了解按位异或(^)运算符的工作原理。

Input Flag Input ^ Flag
0 0 0
0 1 1
1 0 1
1 1 0

可以观察到,当flag的值为1时,输入的值会被反转。

考虑数字57。57的二进制展开式是111001。

1 1 1 0 0 1

考虑一个新的数字1。

0 0 0 0 0 1

要切换最低有效位或最左边的位,请执行57 ^ 1,结果为

1 1 1 0 0 0

The number 111000 is generated.

华友协同办公自动化OA系统
华友协同办公自动化OA系统

华友协同办公管理系统(华友OA),基于微软最新的.net 2.0平台和SQL Server数据库,集成强大的Ajax技术,采用多层分布式架构,实现统一办公平台,功能强大、价格便宜,是适用于企事业单位的通用型网络协同办公系统。 系统秉承协同办公的思想,集成即时通讯、日记管理、通知管理、邮件管理、新闻、考勤管理、短信管理、个人文件柜、日程安排、工作计划、工作日清、通讯录、公文流转、论坛、在线调查、

下载

现在,为了切换最后一位,我们修改数字1,使得最后一位被设置,而不是第一位。为了做到这一点,我们必须将1左移log2(n)位,或者在这种情况下是log2(57),也就是5。在这样做之后,我们得到:

1 0 0 0 0 0

Computing XOR now gives us.

0 1 1 0 0 0

生成了数字01100,等于24。将其与原始数字57的二进制展开进行比较,可以观察到最终答案中的第一位和最后一位已经被切换。

因此,答案是24。

算法

函数 toggle_first_bit()

  • Compute n ^ 1

  • 更新 n

函数 toggle_last_bit()

  • 初始化 r = log2(n)

  • 初始化 k = 1

  • 计算 n ^ k

  • 更新 n

Function main()

  • 初始化 n

  • 如果 (n == 1)

    • return 0;

  • Function call toggle_first_bit().

  • 调用函数 toggle_last_bit()。

  • 显示 n.

示例:C++程序

This program modifies an input number n by toggling the first and the last bit of its binary expansion. It employs bitwise operator XOR and left shift operator to achieve its goal.

// This C++ program toggles the first and the last bit of a number
#include 
#include 
using namespace std;
// this function flips the last bit of the number
// it uses the concept that a log(n) bits are used in the binary expansion of a number n
void toggle_last_bit(int& n){
   int r = log2(n); // value of r indicates the count of last bit of n
   int k; // generate a number with log(n) where only the last bit is 1 using the left shift operator
   k = 1 << r;
   n = n ^ k; // toggle the last bit of n by computing n XOR k
}

// this function flips the first bit of the number by computing n XOR 1
void toggle_first_bit(int& n){
   n = n ^ 1;
}
int main(){
   int n = 113;
   cout << "input number = 113" << endl;
   if(n == 1){
      cout << "0";
      return 0;
   }
   toggle_first_bit(n);  // function call to toggle first bit
   toggle_last_bit(n); // function call to toggle last bit
   cout << "Number after Toggle First and Last Bits of a Number: "<

输出

input number = 113
Number after Toggle First and Last Bits of a Number: 48

Time and Space Analysis

时间复杂度 - O(1),因为该算法始终在常数时间内工作,与输入数字无关。

空间复杂度 - O(1),因为在实现中没有使用辅助空间。

Conclusion

本文讨论了一种切换数字的第一个和最后一个位的方法。为了实现这一点,我们使用了位左移运算符来生成新的位模式,使用位异或运算符来计算结果。为了更深入地理解,详细解释了方法的概念、示例的演示、使用的算法、C++程序解决方案以及时间和空间复杂度分析。

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
java基础知识汇总
java基础知识汇总

java基础知识有Java的历史和特点、Java的开发环境、Java的基本数据类型、变量和常量、运算符和表达式、控制语句、数组和字符串等等知识点。想要知道更多关于java基础知识的朋友,请阅读本专题下面的的有关文章,欢迎大家来php中文网学习。

1500

2023.10.24

Go语言中的运算符有哪些
Go语言中的运算符有哪些

Go语言中的运算符有:1、加法运算符;2、减法运算符;3、乘法运算符;4、除法运算符;5、取余运算符;6、比较运算符;7、位运算符;8、按位与运算符;9、按位或运算符;10、按位异或运算符等等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

231

2024.02.23

php三元运算符用法
php三元运算符用法

本专题整合了php三元运算符相关教程,阅读专题下面的文章了解更多详细内容。

87

2025.10.17

if什么意思
if什么意思

if的意思是“如果”的条件。它是一个用于引导条件语句的关键词,用于根据特定条件的真假情况来执行不同的代码块。本专题提供if什么意思的相关文章,供大家免费阅读。

775

2023.08.22

function是什么
function是什么

function是函数的意思,是一段具有特定功能的可重复使用的代码块,是程序的基本组成单元之一,可以接受输入参数,执行特定的操作,并返回结果。本专题为大家提供function是什么的相关的文章、下载、课程内容,供大家免费下载体验。

482

2023.08.04

js函数function用法
js函数function用法

js函数function用法有:1、声明函数;2、调用函数;3、函数参数;4、函数返回值;5、匿名函数;6、函数作为参数;7、函数作用域;8、递归函数。本专题提供js函数function用法的相关文章内容,大家可以免费阅读。

163

2023.10.07

点击input框没有光标怎么办
点击input框没有光标怎么办

点击input框没有光标的解决办法:1、确认输入框焦点;2、清除浏览器缓存;3、更新浏览器;4、使用JavaScript;5、检查硬件设备;6、检查输入框属性;7、调试JavaScript代码;8、检查页面其他元素;9、考虑浏览器兼容性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

185

2023.11.24

页面置换算法
页面置换算法

页面置换算法是操作系统中用来决定在内存中哪些页面应该被换出以便为新的页面提供空间的算法。本专题为大家提供页面置换算法的相关文章,大家可以免费体验。

407

2023.08.14

Python 自然语言处理(NLP)基础与实战
Python 自然语言处理(NLP)基础与实战

本专题系统讲解 Python 在自然语言处理(NLP)领域的基础方法与实战应用,涵盖文本预处理(分词、去停用词)、词性标注、命名实体识别、关键词提取、情感分析,以及常用 NLP 库(NLTK、spaCy)的核心用法。通过真实文本案例,帮助学习者掌握 使用 Python 进行文本分析与语言数据处理的完整流程,适用于内容分析、舆情监测与智能文本应用场景。

10

2026.01.27

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
进程与SOCKET
进程与SOCKET

共6课时 | 0.4万人学习

Go语言教程-全程干货无废话
Go语言教程-全程干货无废话

共100课时 | 9.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号