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: 24

Binary representation: 00011000

Thus it's a palindrome

Input number 153:

Binary representation: 10011001

Thus it's a palindrome

Input number: 25

Binary representation: 00011001

Thus 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 number

3) Use bitwise operators to convert into binary from

a) Initialize i to 7 (8-1)

b) Find ith bit & store in the array

array[i]=n & 1

c) Right shift n & decrement i

n=n>>1

d) IF n=0

Break

ELSE

Repeat step b-d

4) Check the array where the binary representation is stored,

for palindrome

a) Set j= 0 & k=7 (8-1, 8 is the MAX SIZE)

b) IF (array [j]!= array [k])

It's not a palindrome

c) IF j < k

Increment j& decrement k

Repeat b, c

ELSE

It's a palindrome

Example with explanation:

带有说明的示例:

Input no: 24

Converting to binary representation using bitwise operator

Initially,

N=24

Array[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=12

i=6

-----------------------------------------------------------------

second iteration,

array [6]=n&1 = 0

0000000 (current bit)0

n=n>>1=6

i=5

-----------------------------------------------------------------

third iteration,

array [5]=n&1 = 0

000000 (current bit)0 0

n=n>>1=3

i=4

-----------------------------------------------------------------

fourth iteration,

array [4]=n&1 = 1

00001(current bit)0 0 0

n=n>>1=1

i=3

-----------------------------------------------------------------

fifth iteration,

array [3]=n&1 = 1

0001 (current bit)10 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

0001 10 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

jSecond iteration

Array[j] == array [k] (both 0)

j=j+1=2

k=k-1=5

jThird iteration

Array[j] == array [k] (both 0)

j=j+1=3

k=k-1=4

jFourth 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

C Implementation

C实施

#include

#defineSIZE 8

int 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

printf("%d",c[j]);

printf("\n");

for(int j=0,k=SIZE-1;j

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 palindrome

Second run:

enter the no ( max range 255)

24

binary representation is: 00011000

it's palindrome

Third 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语言位运算符的使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值