And option a. mask <- 0x1 << pos means mask will get the value ( of 0x1 after left shift by pos times)

Please explain it

The Gateway to Computer Science Excellence

First time here? Checkout the FAQ!

x

+16 votes

Consider a new instruction named branch-on-bit-set (mnemonic bbs). The instruction “bbs reg, pos, label” jumps to label if bit in position pos of register operand reg is one. A register is $32 -bits$ wide and the bits are numbered 0 to 31, bit in position 0 being the least significant. Consider the following emulation of this instruction on a processor that does not have bbs implemented.

$temp\leftarrow reg \& mask$

Branch to label if temp is non-zero. The variable temp is a temporary register. For correct emulation, the variable mask must be generated by

- $ mask\leftarrow \text{0x1} << pos$
- $ mask\leftarrow \text{0xffffffff} << pos$
- $ mask\leftarrow pos $
- $ mask\leftarrow \text{0xf}$

+22 votes

Best answer

a. $mask\leftarrow \text{0x1} << pos$

We want to check for a particular bit position say 2 (third from right). Let the number be 0xA2A7 (last 4 bits being 0111). Here, the bit at position 2 from right is 1. So, we have to AND this with 0x0004 as any other flag would give wrong value (may count other bits or discard the bit at position "pos"). And 0x0004 is obtained by 0x1 << 2 (by shifting 1 "pos" times to the left we get a flag with 1 being set only for the "pos" bit position).

We want to check for a particular bit position say 2 (third from right). Let the number be 0xA2A7 (last 4 bits being 0111). Here, the bit at position 2 from right is 1. So, we have to AND this with 0x0004 as any other flag would give wrong value (may count other bits or discard the bit at position "pos"). And 0x0004 is obtained by 0x1 << 2 (by shifting 1 "pos" times to the left we get a flag with 1 being set only for the "pos" bit position).

+10 votes

This question can be simply frame into bit manipulation question.

We can say that "find the bit is set or not at position "pos" in given register "reg".

So finding the bit is set or not simply we need to perform an "AND" operation with '1' of that bit if result is 0 then bit is not set and if result is not 0 then bit is set. Now if given "pos" is not 0 then we need to left shift 1 by pos ( 0x1 << pos ).

+1 vote

here, bbs reg ,pos, label is given instruction , suppose i m taking position =2 in pos register ,

then, "bbs , reg, pos,label" will take branch when 2nd bit of binary pattern stored in register is 1.

but how to check 2nd bit of 32 bit binary pattern in register reg is 1 or not ?

for that we will perform an operation

reg : ..........0......11 (32 bit operand in binary pattern)

mask: 000........0010(32 bit binary pattern in which 2nd bit is 1)

if AND operation is done b/w reg and mask and 2nd bit of result is 1 and all are 0's then branch is taken otherwise brach is not taken

now , question is asking about which one instruction is used to generate mask.

mask <----- 0x1 i.e. (0001)<<(left shift by given position) POS

after shifting 0010 will be stored in mask (32 bit register ) for that option (a) is correct option

then, "bbs , reg, pos,label" will take branch when 2nd bit of binary pattern stored in register is 1.

but how to check 2nd bit of 32 bit binary pattern in register reg is 1 or not ?

for that we will perform an operation

reg : ..........0......11 (32 bit operand in binary pattern)

mask: 000........0010(32 bit binary pattern in which 2nd bit is 1)

if AND operation is done b/w reg and mask and 2nd bit of result is 1 and all are 0's then branch is taken otherwise brach is not taken

now , question is asking about which one instruction is used to generate mask.

mask <----- 0x1 i.e. (0001)<<(left shift by given position) POS

after shifting 0010 will be stored in mask (32 bit register ) for that option (a) is correct option

- All categories
- General Aptitude 1.3k
- Engineering Mathematics 5.5k
- Digital Logic 2.1k
- Programming & DS 4k
- Algorithms 3.4k
- Theory of Computation 4.2k
- Compiler Design 1.6k
- Databases 3.1k
- CO & Architecture 2.7k
- Computer Networks 3.1k
- Non GATE 1.1k
- Others 1.4k
- Admissions 501
- Exam Queries 449
- Tier 1 Placement Questions 19
- Job Queries 62
- Projects 12

38,079 questions

45,572 answers

132,069 comments

49,045 users