--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)哪个不是按位运算符?

  1. &

  2. |

    |

  3. <<

    <<

  4. &&

    &&

Answer & Explanation 答案与解释

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;
}

  1. c= 12

    c = 12

  2. c= 10

    c = 10

  3. c= 2

    c = 2

  4. c= 0

    c = 0

Answer & Explanation 答案与解释

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 1010
    b=2 //0000 0010
    Doing bitwise AND
    0000 0010// 2

For 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   0x02

int 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;
}

  1. I have purchased ...:

    我已购买...:

  2. I have purchased ...:Mobile, Lappy

    我已购买...:手机,Lappy

  3. I have purchased ...:Mobile,

    我已购买...:手机,

  4. I have purchased ...:Lappy

    我已购买...:蓝宝

Answer & Explanation 答案与解释

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)条件都将成立。

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;
}

  1. 8,9

    8,9

  2. 4,5

    4,5

  3. 8,8

    8,8

  4. 4,4

    4,4

Answer & Explanation 答案与解释

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;
}

  1. 13

    13

  2. d

    d

  3. 22

    22

  4. 10

    10

Answer & Explanation 答案与解释

Correct answer: 1
13

正确答案:1
13

Consider the expression:

考虑以下表达式:

    flag = 0x0f //0000 1111
    flag &= ~0x02
    flag = flag & (~0x02) //since ~ has precedence over ‘&’ operator
    0x02 = 0000 0010
    ~0x02 = 1111 1101
    flag & ~0x02
    = 0000 1111 & 1111 1101
    = 0000 1101
    = 0x0D

But 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 ^ 2

What will be the value of x?

x的值是多少?

  1. 5

    5

  2. 6

    6

  3. 7

    7

  4. 8

    8

Answer & Explanation 答案与解释

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       0

Here, 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

7) Predict the output of following program.

7)预测以下程序的输出。

#include <stdio.h>

int main()
{
    int x=10;
    
    x &= ~2;
    printf("x= %d",x);
    
    return 0;
}

  1. x= 10

    x = 10

  2. x= 8

    x = 8

  3. x= 12

    x = 12

  4. x= 0

    x = 0

Answer & Explanation 答案与解释

Correct answer: 2
x= 8

正确答案:2
x = 8

Consider the explanation:

考虑以下解释:

    x =10//0000 1010
    x &= ~2
    x = x & (~2) //since ~ has precedence over ‘&’ operator
    2 = 0000 0010
    ~2 = 1111 1101
    x & ~0x02
    = 0000 1010& 1111 1101
    = 0000 1000
    = 8

The 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)哪个位运算符可用于快速检查数字是偶数还是奇数?

  1. Bitwise AND (&)

    按位与(&)

  2. Bitwise OR (|)

    按位或(|)

  3. Bitwise XOR (^)

    按位XOR(^)

  4. Bitwise NOT (~)

    按位非(〜)

Answer & Explanation 答案与解释

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);
        else
            printf("%2d is EVEN number\n",count);
    
    return 0;
}

Output

输出量

 1 is ODD number
 2 is EVEN number
 3 is ODD number
 4 is EVEN number
 5 is ODD number
 6 is EVEN number
 7 is ODD number
 8 is EVEN number
 9 is ODD number
10 is EVEN number

9) Which statement is suitable to check 3rd (count from 0) bit is high (set) or not?

9),该语句是合适的,以检查第三 ( 从0)计数 (组)或不?

  1. (num & (1<<3))

    (数字&(1 << 3))

  2. (num & 0x08)

    (数字和0x08)

  3. (num & 0x03)

    (数字和0x03)

  4. Both (1) and (2)

    (1)和(2)

Answer & Explanation 答案与解释

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");
    else
        printf("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 Low

Binary 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...

选择正确的单词...

  1. Multiplication and Division

    乘法与除法

  2. Division and Multiplication

    除法与乘法

  3. Multiplication and Remainder

    乘法与余数

  4. Remainder and Multiplication

    余数与乘法

Answer & Explanation 答案与解释

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语言运算符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值