--c语言运算符_C按位运算符-能力问题和解答
--c語言運算符
C programming Bitwise Operators Aptitude Questions and Answers: In this section you will find C Aptitude Questions and Answers on Bitwise Operators like Bitwise OR (|), Bitwise AND (&), Bitwise NOT (!).
C編程按位運算符的天賦問題和答案:在本節中,您將找到按位運算符(如按位OR(|),按位與(&),按位NOT(!))上的C適度問題和答案。
1) Which is not a bitwise operator?
1)哪個不是按位運算符?
&
和
|
|
<<
<<
&&
&&
Correct answer: 4
&&
正確答案:4
&&
&& is not a bitwise operator. It is a Logical AND operator, which is used to check set of conditions (more than one condition) together, if all conditions are true it will return 1 else it will return 0.
&&不是按位運算符。 這是一個邏輯AND運算符 ,用于一起檢查一組條件(多個條件),如果所有條件都為true,則將返回1,否則將返回0。
2) Predict the output of following program.
2)預測以下程序的輸出。
#include <stdio.h>int main() {int a=10;int b=2;int c;c=(a & b);printf("c= %d",c);return 0; }c= 12
c = 12
c= 10
c = 10
c= 2
c = 2
c= 0
c = 0
Correct answer: 3
c= 2
正確答案:3
c = 2
Bitwise AND (&), It does AND on every bits of two numbers. The result of AND is 1 only if both bits are 1.
按位與( & ),它對兩個數字的每個位執行與。 僅當兩個位均為1時,AND的結果才為1。
a=10 //0000 1010b=2 //0000 0010Doing bitwise AND0000 0010// 2For details on bitwise operator refer to Bitwise Operators and their working with Examples in C
有關按位運算符的詳細信息,請參閱按位運算符及其在C中的示例工作
3) Predict the output of following program.
3)預測以下程序的輸出。
#include <stdio.h>#define MOBILE 0x01 #define LAPPY 0x02int main() {unsigned char item=0x00;item |=MOBILE;item |=LAPPY;printf("I have purchased ...:");if(item & MOBILE){printf("Mobile, ");}if(item & LAPPY){printf("Lappy");}return 1; }I have purchased ...:
我已購買...:
I have purchased ...:Mobile, Lappy
我已購買...:手機,Lappy
I have purchased ...:Mobile,
我已購買...:手機,
I have purchased ...:Lappy
我已購買...:藍寶
Correct answer: 2
I have purchased ...:Mobile, Lappy
正確答案:2
我已購買...:手機,Lappy
Bitwise OR (|) operator copies bit(s), if they are exist either side of the operands (that means if any bit is exist in any operand). Here, binary of Macro MOBILE (0x01) is "0001" and binary of Macro LAPPY (0x02) is "0010", then result of the expression item |=MOBILE; will be "0001" and second expression item |=LAPPY; will return "0011". Thus, both conditions (item & MOBILE) and (item & LAPPY) will be true.
如果位存在于操作數的任一側(即任何操作數中是否存在任何位), 則按位OR ( | )運算符將復制位。 此處,宏MOBILE (0x01)的二進制為“ 0001” ,宏LAPPY (0x02)的二進制為“ 0010” ,則表達式項的結果| = MOBILE; 將為“ 0001” ,第二個表達式項| = LAPPY; 將返回“ 0011” 。 因此, (item和MOBILE)條件和(item&LAPPY)條件都將成立。
.minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}4) Predict the output of following program.
4)預測以下程序的輸出。
#include <stdio.h>int main() {char var=0x04;var = var | 0x04;printf("%d,",var);var |= 0x01;printf("%d",var);return 0; }8,9
8,9
4,5
4,5
8,8
8,8
4,4
4,4
Correct answer: 2
4,5
正確答案:2
4,5
Value of var is 0x04 (0100), Consider the expression var = var | 0x04 The OR (|) of 0100, 0100 is 0100, hence value will remain 0100. After the expression var |=0x01, value will be 0101 that is 0x05.
var的值為0x04(0100) ,請考慮表達式var = var | 0x04 0100的OR(|) ,0100是0100,因此值將保持為0100。在表達式var | = 0x01之后 ,值將是0101,即0x05。
5) Predict the output of following program.
5)預測以下程序的輸出。
#include <stdio.h>int main() {char flag=0x0f;flag &= ~0x02;printf("%d",flag);return 0; }13
13
d
d
22
22
10
10
Correct answer: 1
13
正確答案:1
13
Consider the expression:
考慮以下表達式:
flag = 0x0f //0000 1111flag &= ~0x02flag = flag & (~0x02) //since ~ has precedence over ‘&’ operator0x02 = 0000 0010~0x02 = 1111 1101flag & ~0x02= 0000 1111 & 1111 1101= 0000 1101= 0x0DBut since the placeholder in printf is %d, it prints the decimal value 13.
但是由于printf中的占位符為%d ,因此它將輸出十進制值13 。
6) Consider the given statement:
6)考慮給定的陳述:
int x = 10 ^ 2What will be the value of x?
x的值是多少?
5
5
6
6
7
7
8
8
Correct answer: 4
8
正確答案:4
8
XOR operator (^) copies bit(s), if one operand has 1 and other has 0, consider the given truth table:
XOR運算符( ^ )復制一個或多個位,如果一個操作數為1,另一個操作數為0,則考慮給定的真值表:
a b (a^b) _______________________0 0 0 0 1 1 1 0 1 1 1 0Here, binary of 10 is "1010" and binary of 2 is "0010", then the result of statement (10 ^ 2) will be "1000", which is equivalent to 8 in decimal.
在這里,二進制10為“ 1010” ,二進制2為“ 0010” ,則語句(10 ^ 2)的結果為“ 1000” ,相當于十進制的8 。
.minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}7) Predict the output of following program.
7)預測以下程序的輸出。
#include <stdio.h>int main() {int x=10;x &= ~2;printf("x= %d",x);return 0; }x= 10
x = 10
x= 8
x = 8
x= 12
x = 12
x= 0
x = 0
Correct answer: 2
x= 8
正確答案:2
x = 8
Consider the explanation:
考慮以下解釋:
x =10//0000 1010x &= ~2x = x & (~2) //since ~ has precedence over ‘&’ operator2 = 0000 0010~2 = 1111 1101x & ~0x02= 0000 1010& 1111 1101= 0000 1000= 8The statement x &= ~2; will clear second bit from the value of 10, binary of x is "1010" and the binary of 2 is "0010", thus this statement will clear second bit and returns "1000" that is equivalent to 8 in Decimal.
語句x&=?2; 將從值10清除第二個位, x的二進制為“ 1010” , 2的二進制為“ 0010” ,因此此語句將清除第二個位并返回與十進制中的8等效的“ 1000” 。
8) Which Bitwise Operator can be used to check whether a number is EVEN or ODD quickly?
8)哪個位運算符可用于快速檢查數字是偶數還是奇數?
Bitwise AND (&)
按位與(&)
Bitwise OR (|)
按位或(|)
Bitwise XOR (^)
按位XOR(^)
Bitwise NOT (~)
按位非(?)
Correct answer: 1
Bitwise AND (&)
正確答案:1
按位與(&)
Bitwise AND (&) Operator can be used to check whether a number if EVEN or ODD, consider the statement (num & 1), this statement will return 1 if first bit of the number is High (1) else it will return 0. All ODD numbers have their firs bit 1 and ODD numbers have 0.
可以使用按位AND(&)運算符來檢查一個數字是偶數還是奇數,請考慮語句(num&1) ,如果數字的第一位為High(1),則此語句將返回1,否則將返回0。所有ODD編號的第一個比特為1,而ODD編號的第一個比特為0。
Consider the following program:
考慮以下程序:
#include <stdio.h>int main() {int count;for(count=1; count<=10; count+=1)if(count & 1)printf("%2d is ODD number\n",count);elseprintf("%2d is EVEN number\n",count);return 0; }Output
輸出量
1 is ODD number2 is EVEN number3 is ODD number4 is EVEN number5 is ODD number6 is EVEN number7 is ODD number8 is EVEN number9 is ODD number 10 is EVEN number9) Which statement is suitable to check 3rd (count from 0) bit is high (set) or not?
9),該語句是合適的,以檢查第三 ( 從0)位計數是高 (組)或不?
(num & (1<<3))
(數字&(1 << 3))
(num & 0x08)
(數字和0x08)
(num & 0x03)
(數字和0x03)
Both (1) and (2)
(1)和(2)
Correct answer: 4
Both (1) and (2)
正確答案:4
(1)和(2)
The value of (1<<3) is 8 in Decimal and value of 0x08 is 8 in Decimal, both statements are suitable to check whether 3rd bit of num is High (set) or not.
(1 << 3)的值為8和十進制的0x08的值在十進制8,這兩個語句適合于檢查NUM的第三位是否為高(組)或沒有。
Consider this program:
考慮以下程序:
#include <stdio.h>int main() {int num;printf("Enter an integer number: ");scanf("%d",&num);if(num & (1<<3))printf("3rd bit is High (Set)\n");elseprintf("3rd bit is Low\n");return 0; }Output
輸出量
First run: Enter an integer number: 15 3rd bit is High (Set) Second run: Enter an integer number: 7 3rd bit is LowBinary of 15 is: 1111 & Binary of 7 is: 0111, thus in first case 3rd bit is high and in second case 3rd bit is low. [Count from 0]
的15二進制是:1111的7個二進制是:0111,從而在第一種情況下第三位為高,在第二種情況下第三位為低。 [從0開始計數]
10) Left shift (<>) operators are equivalent to _____________ by 2.
10)左移(<>)運算符等于_____________ 2。
Choose the correct words...
選擇正確的單詞...
Multiplication and Division
乘法與除法
Division and Multiplication
除法與乘法
Multiplication and Remainder
乘法與余數
Remainder and Multiplication
余數與乘法
Correct answer: 1
Multiplication and Division
正確答案:1
乘法與除法
Left shift by 1 return the multiplication by 2 and Right shift by 1 return the division by 2.
左移1返回乘以2,右移1返回除以2。
Consider this program:
考慮以下程序:
#include <stdio.h>int main() {int num;printf("Enter an integer number: ");scanf("%d",&num);printf("Multiplication by 2 = %d\n", (num<<1));printf("Division by 2 = %d\n",(num>>1));return 0; }Output
輸出量
Enter an integer number: 100 Multiplication by 2 = 200 Division by 2 = 50翻譯自: https://www.includehelp.com/c/bitwise-operators-aptitude-questions-and-answers.aspx
--c語言運算符
總結
以上是生活随笔為你收集整理的--c语言运算符_C按位运算符-能力问题和解答的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: String.IsNullOrEmpty
- 下一篇: 认真聊一下MySQL索引的底层实现!