700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > c语言位运算符的使用_C语言程序使用位运算符检查给定数字是否为回文

c语言位运算符的使用_C语言程序使用位运算符检查给定数字是否为回文

时间:2019-09-25 02:27:37

相关推荐

c语言位运算符的使用_C语言程序使用位运算符检查给定数字是否为回文

c语言位运算符的使用

Problem statement:Write aC program to check whether a number (binary representation) is palindrome or not using bitwise operators. Maximum input is 255..

问题陈述:编写一个C程序以使用按位运算符检查数字(二进制​​表示形式)是否为回文。最大输入为255。

Solution:We can use bitwise operator here to solve the problem.

解决方案:我们可以在这里使用按位运算符来解决问题。

Pre-requisite: Input numbern

前提条件:输入数字n

Input Example:

输入示例:

Input number: 24Binary representation: 00011000Thus it's a palindromeInput number 153:Binary representation: 10011001Thus it's a palindromeInput number: 25Binary representation: 00011001Thus it's not a palindrome

Algorithm:

算法:

1) Take the input.2) Create an array of length 8 to store 8 bit binary representation of input number3) Use bitwise operators to convert into binary froma) Initialize i to 7 (8-1)b) Find ith bit & store in the arrayarray[i]=n & 1c) Right shift n & decrement in=n>>1d) IF n=0BreakELSERepeat step b-d4) Check the array where the binary representation is stored, for palindromea) Set j= 0 & k=7 (8-1, 8 is the MAX SIZE)b) IF (array [j]!= array [k])It's not a palindromec) IF j < kIncrement j& decrement kRepeat b, cELSEIt's a palindrome

Example with explanation:

带有说明的示例:

Input no: 24Converting to binary representation using bitwise operatorInitially,N=24Array[8]={0};00000000 (LSB)i=7-----------------------------------------------------------------first iteration,array[7]=n&1 = 0 (refer to bitwise operators and their working for understanding the outcome)00000000 (current bit)n=n>>1=12i=6-----------------------------------------------------------------second iteration,array [6]=n&1 = 0 0000000 (current bit)0 n=n>>1=6i=5-----------------------------------------------------------------third iteration,array [5]=n&1 = 0 000000 (current bit)0 0 n=n>>1=3i=4-----------------------------------------------------------------fourth iteration,array [4]=n&1 = 1 00001(current bit)0 0 0 n=n>>1=1i=3-----------------------------------------------------------------fifth iteration,array [3]=n&1 = 1 0001 (current bit)10 0 0 n=n>>1=0i=2-----------------------------------------------------------------sixth iteration,n=0so no more processingthus the array finally becomes which is the binary representation0001 10 0 0 (LSB)Checking palindromeInitially,j=0 & k=7 (8-1)-----------------------------------------------------------------First iterationArray[j] == array [k] (both 0)j=j+1=1k=k-1=6j<k-----------------------------------------------------------------Second iterationArray[j] == array [k] (both 0)j=j+1=2k=k-1=5j<k-----------------------------------------------------------------Third iterationArray[j] == array [k] (both 0)j=j+1=3k=k-1=4j<k-----------------------------------------------------------------Fourth iterationArray[j] == array [k] (both 1)j=j+1=4k=k-1=3j>kthus, stops processing & prints it's a palindrome

C Implementation

C实施

#include <stdio.h>#define SIZE 8int main(){unsigned int n;printf("enter the no ( max range 255)\n");scanf("%d",&n);int c[SIZE]={0};int i=SIZE-1;printf("binary representation is: ");while(n!=0){c[i--]=n&1;n=n>>1;}for(int j=0;j<SIZE;j++)printf("%d",c[j]);printf("\n");for(int j=0,k=SIZE-1;j<k;j++,k--){if(c[j]!=c[k]){printf("Not palindrome\n");return 0;}}printf("it's palindrome\n");return 0;}

Output

输出量

First run:enter the no ( max range 255)153binary representation is: 10011001it's palindromeSecond run:enter the no ( max range 255)24binary representation is: 00011000it's palindromeThird run:enter the no ( max range 255)-8binary representation is: 11111000Not palindrome

翻译自: /c-programs/check-number-is-palindrome-or-not-using-bitwise-operator.aspx

c语言位运算符的使用

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。