search
HomeBackend DevelopmentC++C++ program to convert binary number to Gray code using recursion
C++ program to convert binary number to Gray code using recursionSep 16, 2023 pm 01:57 PM
binaryrecursionGray code

C++ program to convert binary number to Gray code using recursion

Gray code or reflected binary code is a special type of binary representation of a number in which two consecutive values ​​differ in only one bit. For example, the binary equivalents of 1 and 2 are 01 and 10, where two bits are changing. But in Gray code, 1 is 01, 2 is 11, and only one bit is changing. In this article, we will see how to convert a given binary number to its Gray code equivalent using recursion in C.

Pass numbers as decimal integers

In the first example, we provide decimal numbers. The numbers are only 0 and 1, but the numbers are in decimal. For example, if we want to pass 6 as input, we pass 110 (one hundred and ten in decimal), which is equivalent to 6 in binary. The program returns output similarly.

algorithm

  • Define a function solve(), which will take a binary number
  • If n is 0, then
    • Return 0
  • If it ends
  • Last := the last digit of n
  • second_last = the penultimate digit of n
  • If the last digit and the penultimate digit are different, then
    • Enter 1 and call solve(n cuts the last bit)
  • otherwise
    • Enter 0 and call solve(n cuts the last bit)
  • If it ends
  • solve() function ends

Example

#include <iostream>
using namespace std;

int solve( int n ) {
   if( n == 0 )
   return 0;
   int last = n % 10;
   int second_last = (n / 10) % 10;
   if( (last && !second_last) || (!last && second_last) ) {
      return (1 + 10 * solve( n / 10 ));
   }
   return (10 * solve( n / 10 ));
}

int main()
{
   cout << "Gray code for the number 2 (10) is: " << solve( 10 ) << endl;
   cout << "Gray code for the number 6 (110) is: " << solve( 110 ) << endl;
   cout << "Gray code for the number 13 (1101) is: " << solve( 1101 ) << endl;
   cout << "Gray code for the number 93 (1011101) is: " << solve( 1011101 ) << endl;
}

Output

Gray code for the number 2 (10) is: 11
Gray code for the number 6 (110) is: 101
Gray code for the number 13 (1101) is: 1011
Gray code for the number 93 (1011101) is: 1110011

in conclusion

Gray code or reflected binary code can be found by applying XOR operation to consecutive bits. The same thing is achieved by taking the last two digits of the given number, when they are not the same, call the function recursively and pass the number except the last digit, the result will be concatenated with 1, otherwise with 0, etc. And so on. In the example, we have provided input as an integer decimal number and the output is also in integer decimal format. The same problem can be solved by taking a string type input which can be used to provide larger input when required.

The above is the detailed content of C++ program to convert binary number to Gray code using recursion. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
如何解决Python的最大递归深度错误?如何解决Python的最大递归深度错误?Jun 24, 2023 pm 02:48 PM

Python是一门易学易用的编程语言,然而在使用Python编写递归函数时,可能会遇到递归深度过大的错误,这时就需要解决这个问题。本文将为您介绍如何解决Python的最大递归深度错误。1.了解递归深度递归深度是指递归函数嵌套的层数。在Python默认情况下,递归深度的限制是1000,如果递归的层数超过这个限制,系统就会报错。这种报错通常称为“最大递归深度错误

二进制算法怎么算二进制算法怎么算Jan 19, 2024 pm 04:38 PM

二进制算法是一种基于二进制数的运算方法,其基本运算包括加法、减法、乘法和除法。除了基本运算外,二进制算法还包括逻辑运算、位移运算等操作。逻辑运算包括与、或、非等操作,位移运算包括左移和右移操作。这些操作都有对应的规则和操作数的要求。

如何使用C语言将二进制转换为十六进制?如何使用C语言将二进制转换为十六进制?Sep 01, 2023 pm 06:57 PM

二进制数以1和0表示。16位的十六进制数系统为{0,1,2,3…..9,A(10),B(11),……F(15)}为了从二进制表示转换为十六进制表示,位串id被分组为4位块,从最低有效侧开始称为半字节。每个块都替换为相应的十六进制数字。让我们看一个示例,以清楚地了解十六进制和二进制数字表示。001111100101101100011101&nbsp;3&nbsp;&nbsp;E&nbsp;&nbsp;5&nbsp;&nbsp;B&nb

EDVAC有哪两个重大的改进EDVAC有哪两个重大的改进Mar 02, 2023 pm 02:58 PM

EDVAC的两个重大的改进:一是采用二进制,二是完成了存贮程序,可以自动地从一个程序指令进到下一个程序指令,其作业可以通过指令自动完成。“指令”包括数据和程序,把它们用码的形式输入到机器的记忆装置中,即用记忆数据的同一记忆装置存贮执行运算的命令,这就是所谓存贮程序的新概念。

Golang能否处理二进制文件?Golang能否处理二进制文件?Mar 20, 2024 pm 04:36 PM

Golang能否处理二进制文件?在Go语言中,处理二进制文件是非常常见且方便的。通过使用内置的包和方法,我们可以轻松地读取、写入和操作二进制文件。本文将介绍如何在Go中处理二进制文件,并提供具体的代码示例。读取二进制文件要读取一个二进制文件,我们首先需要打开这个文件并创建一个对应的文件对象。然后,我们可以使用Read方法从文件中读取数据,并以字节的形式存储在

Golang如何读取二进制文件?Golang如何读取二进制文件?Mar 21, 2024 am 08:27 AM

Golang如何读取二进制文件?二进制文件是以二进制形式存储的文件,其中包含了计算机能够识别和处理的数据。在Golang中,我们可以使用一些方法来读取二进制文件,并将其解析成我们想要的数据格式。下面将介绍如何在Golang中读取二进制文件,并给出具体的代码示例。首先,我们需要使用os包中的Open函数打开一个二进制文件,这将返回一个文件对象。然后,我们可以使

轻松学会Go语言中16进制转二进制轻松学会Go语言中16进制转二进制Mar 15, 2024 pm 04:45 PM

题目:轻松学会Go语言中16进制转二进制,需要具体代码示例在计算机编程中,经常会涉及到对不同进制数之间的转换操作。其中,16进制和二进制之间的转换是比较常见的。在Go语言中,我们可以通过一些简单的代码示例来实现16进制到二进制的转换,让我们一起来学习一下。首先,我们来了解一下16进制和二进制的表示方法。16进制是一种表示数字的方法,使用0-9和A-F来表示1

Go语言中的循环和递归的比较研究Go语言中的循环和递归的比较研究Jun 01, 2023 am 09:23 AM

注:本文以Go语言的角度来比较研究循环和递归。在编写程序时,经常会遇到需要对一系列数据或操作进行重复处理的情况。为了实现这一点,我们需要使用循环或递归。循环和递归都是常用的处理方式,但在实际应用中,它们各有优缺点,因此在选择使用哪种方法时需要考虑实际情况。本文将对Go语言中的循环和递归进行比较研究。一、循环循环是一种重复执行某段代码的机制。Go语言中主要有三

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software