c语言位运算符的使用_C语言程序使用位运算符检查给定数字是否为回文
生活随笔
收集整理的這篇文章主要介紹了
c语言位运算符的使用_C语言程序使用位运算符检查给定数字是否为回文
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
c語言位運算符的使用
Problem statement: Write a C 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 number n
前提條件 :輸入數字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 palindromeAlgorithm:
算法:
1) Take the input. 2) Create an array of length 8 to store 8 bit binary representation of input number 3) 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 palindromeExample with explanation:
帶有說明的示例:
Input no: 24 Converting to binary representation using bitwise operatorInitially, N=24 Array[8]={0};0 0 0 0 0 0 0 0 (LSB) i=7 -----------------------------------------------------------------first iteration, array[7]=n&1 = 0 (refer to bitwise operators and their working for understanding the outcome) 0 0 0 0 0 0 0 0 (current bit) n=n>>1=12 i=6 -----------------------------------------------------------------second iteration, array [6]=n&1 = 0 0 0 0 0 0 0 0 (current bit) 0 n=n>>1=6 i=5 -----------------------------------------------------------------third iteration, array [5]=n&1 = 0 0 0 0 0 0 0 (current bit) 0 0 n=n>>1=3 i=4 -----------------------------------------------------------------fourth iteration, array [4]=n&1 = 1 0 0 0 0 1(current bit) 0 0 0 n=n>>1=1 i=3 -----------------------------------------------------------------fifth iteration, array [3]=n&1 = 1 0 0 0 1 (current bit) 1 0 0 0 n=n>>1=0 i=2 -----------------------------------------------------------------sixth iteration, n=0 so no more processing thus the array finally becomes which is the binary representation 0 0 0 1 1 0 0 0 (LSB)Checking palindrome Initially, j=0 & k=7 (8-1) -----------------------------------------------------------------First iteration Array[j] == array [k] (both 0) j=j+1=1 k=k-1=6 j<k -----------------------------------------------------------------Second iteration Array[j] == array [k] (both 0) j=j+1=2 k=k-1=5 j<k -----------------------------------------------------------------Third iteration Array[j] == array [k] (both 0) j=j+1=3 k=k-1=4 j<k -----------------------------------------------------------------Fourth iteration Array[j] == array [k] (both 1) j=j+1=4 k=k-1=3 j>k thus, stops processing & prints it's a palindrome .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}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) 153 binary representation is: 10011001 it's palindromeSecond run: enter the no ( max range 255) 24 binary representation is: 00011000 it's palindromeThird run: enter the no ( max range 255) -8 binary representation is: 11111000 Not palindrome翻譯自: https://www.includehelp.com/c-programs/check-number-is-palindrome-or-not-using-bitwise-operator.aspx
c語言位運算符的使用
總結
以上是生活随笔為你收集整理的c语言位运算符的使用_C语言程序使用位运算符检查给定数字是否为回文的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 卡诺模板_无关条件的卡诺地图
- 下一篇: ruby 执行函数_Ruby at()函