日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c语言给定一个非空整数数组_C程序检查给定整数的所有位是否为一(1)

發布時間:2025/3/11 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言给定一个非空整数数组_C程序检查给定整数的所有位是否为一(1) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

c語言給定一個非空整數數組

Problem statement: Write a C Program to check if all the bits of a given integer is one (1).

問題陳述:編寫一個C程序來檢查給定整數的所有位是否都是一(1) 。

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

解決方案:我們可以在這里使用按位運算符來解決問題。

Pre-requisite: Input number n

前提條件 :輸入數字n

Algorithm:

算法:

1) Do Bitwise AND with n and 1. n & 1let n be a7a6a5a4a3a2a1a01->00000001So doing bitwise AND (refer to published article on bitwise operators) will result in all bits 0 except the LSB which will be a0. If a0 is 0 then the all bits are not setThus,IFn & 1 ==0Print that all bits are not sameELSERight shift n by 1n=n>>1END IF-ELSE2) IF n==0Print that all bits are setELSEREPEAT step 1

Example with Explanation:

解釋示例:

Checking for 12 12-> 00001100So first iteration: n=12 //00001100 n & 1 ==0 so print that all bits are not setChecking for 7 7->00000111So first iteration: n=7 //00000111 n & 1 =1 n=n>>1 (n=3) //00000011So second iteration: n=3 //00000011 n & 1 =1 n=n>>1 (n=1) //00000001So third iteration: n=1 //00000001 n & 1 =1 n=n>>1 (n=0) //00000000So, All bits are set .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>int main() {unsigned int n;printf("enter the integer\n");scanf("%d",&n);while(n>0){int temp=n&1;if(temp == 0){ //if any bit not setprintf("all bits are not set\n");return 0;}n=n>>1; //right shift operator}printf("all bits are set ");printf("in its binary representation\n");return 0; }

Output

輸出量

First run: enter the integer 12 all bits are not setSecond run: enter the integer 7 all bits are set in its binary representation

翻譯自: https://www.includehelp.com/c-programs/check-if-all-the-bits-of-a-given-integer-is-one-1.aspx

c語言給定一個非空整數數組

總結

以上是生活随笔為你收集整理的c语言给定一个非空整数数组_C程序检查给定整数的所有位是否为一(1)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。