--c语言运算符_C按位运算符-能力问题和解答
--c語言運(yùn)算符
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編程按位運(yùn)算符的天賦問題和答案:在本節(jié)中,您將找到按位運(yùn)算符(如按位OR(|),按位與(&),按位NOT(!))上的C適度問題和答案。
1) Which is not a bitwise operator?
1)哪個(gè)不是按位運(yùn)算符?
&
和
|
|
<<
<<
&&
&&
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.
&&不是按位運(yùn)算符。 這是一個(gè)邏輯AND運(yùn)算符 ,用于一起檢查一組條件(多個(gè)條件),如果所有條件都為true,則將返回1,否則將返回0。
2) Predict the output of following program.
2)預(yù)測(cè)以下程序的輸出。
#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.
按位與( & ),它對(duì)兩個(gè)數(shù)字的每個(gè)位執(zhí)行與。 僅當(dāng)兩個(gè)位均為1時(shí),AND的結(jié)果才為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
有關(guān)按位運(yùn)算符的詳細(xì)信息,請(qǐng)參閱按位運(yùn)算符及其在C中的示例工作
3) Predict the output of following program.
3)預(yù)測(cè)以下程序的輸出。
#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
我已購買...:手機(jī),Lappy
I have purchased ...:Mobile,
我已購買...:手機(jī),
I have purchased ...:Lappy
我已購買...:藍(lán)寶
Correct answer: 2
I have purchased ...:Mobile, Lappy
正確答案:2
我已購買...:手機(jī),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.
如果位存在于操作數(shù)的任一側(cè)(即任何操作數(shù)中是否存在任何位), 則按位OR ( | )運(yùn)算符將復(fù)制位。 此處,宏MOBILE (0x01)的二進(jìn)制為“ 0001” ,宏LAPPY (0x02)的二進(jìn)制為“ 0010” ,則表達(dá)式項(xiàng)的結(jié)果| = MOBILE; 將為“ 0001” ,第二個(gè)表達(dá)式項(xiàng)| = 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)預(yù)測(cè)以下程序的輸出。
#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) ,請(qǐng)考慮表達(dá)式var = var | 0x04 0100的OR(|) ,0100是0100,因此值將保持為0100。在表達(dá)式var | = 0x01之后 ,值將是0101,即0x05。
5) Predict the output of following program.
5)預(yù)測(cè)以下程序的輸出。
#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:
考慮以下表達(dá)式:
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 ,因此它將輸出十進(jìn)制值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運(yùn)算符( ^ )復(fù)制一個(gè)或多個(gè)位,如果一個(gè)操作數(shù)為1,另一個(gè)操作數(shù)為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.
在這里,二進(jìn)制10為“ 1010” ,二進(jìn)制2為“ 0010” ,則語句(10 ^ 2)的結(jié)果為“ 1000” ,相當(dāng)于十進(jìn)制的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)預(yù)測(cè)以下程序的輸出。
#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清除第二個(gè)位, x的二進(jìn)制為“ 1010” , 2的二進(jìn)制為“ 0010” ,因此此語句將清除第二個(gè)位并返回與十進(jìn)制中的8等效的“ 1000” 。
8) Which Bitwise Operator can be used to check whether a number is EVEN or ODD quickly?
8)哪個(gè)位運(yùn)算符可用于快速檢查數(shù)字是偶數(shù)還是奇數(shù)?
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(&)運(yùn)算符來檢查一個(gè)數(shù)字是偶數(shù)還是奇數(shù),請(qǐng)考慮語句(num&1) ,如果數(shù)字的第一位為High(1),則此語句將返回1,否則將返回0。所有ODD編號(hào)的第一個(gè)比特為1,而ODD編號(hào)的第一個(gè)比特為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)位計(jì)數(shù)是高 (組)或不?
(num & (1<<3))
(數(shù)字&(1 << 3))
(num & 0x08)
(數(shù)字和0x08)
(num & 0x03)
(數(shù)字和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和十進(jìn)制的0x08的值在十進(jìn)制8,這兩個(gè)語句適合于檢查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二進(jìn)制是:1111的7個(gè)二進(jìn)制是:0111,從而在第一種情況下第三位為高,在第二種情況下第三位為低。 [從0開始計(jì)數(shù)]
10) Left shift (<>) operators are equivalent to _____________ by 2.
10)左移(<>)運(yùn)算符等于_____________ 2。
Choose the correct words...
選擇正確的單詞...
Multiplication and Division
乘法與除法
Division and Multiplication
除法與乘法
Multiplication and Remainder
乘法與余數(shù)
Remainder and Multiplication
余數(shù)與乘法
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語言運(yùn)算符
總結(jié)
以上是生活随笔為你收集整理的--c语言运算符_C按位运算符-能力问题和解答的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: String.IsNullOrEmpty
- 下一篇: 为什么阿里内部不允许用Executors