IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

SHIFT instructions


IBM Mainframe Forums -> Mainframe Interview Questions
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
alokagarwaljgd

New User


Joined: 02 Jun 2006
Posts: 28

PostPosted: Tue Aug 02, 2011 5:34 pm
Reply with quote

Hi,

Can any body help me out with below two questions. It was asked in one of the quiz.

1) What will happen if we shift n bits to left using SLL and then again if we use SRL to shift n bits to right?

2) What does the following instruction do?

EX 0,SIGNWK

SIGNWK MVC VAR+4(2),=X'000C'
XC VAR2(5),VAR3

where VAR, VAR2 and VAR3 can be assumed as already defined valid variables.

Any help for the above is highly appreciated.

Thanks,
Alok
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Tue Aug 02, 2011 5:58 pm
Reply with quote

Try a bit of experimentation.

Open a new member in your editor.

Fill a single line with a single character, all the way to the end of the line.

Do a shift left of a number of characters, like "<5". Then do a shift right ">5".

Same thing is happening. Try it swapping the commands around as well if you like.

For your second question, look up the instructions in the POP, if you don't know them, and get an idea there. Search the Assembler forum for EX (Execute). If you get stuck, let use know where. If you get an answer, let us know as well.
Back to top
View user's profile Send private message
alokagarwaljgd

New User


Joined: 02 Jun 2006
Posts: 28

PostPosted: Wed Aug 03, 2011 1:47 pm
Reply with quote

1) I did the left shift and right shift as suggested by you. I filled the line completely with 1s. Then on doing left shift (let's say 3 times), first time, 3 charcters (say 111) are removed from the line on extreme left side and 3 blank spaces are filled on extreme right side of the line. Again after doing right shift ( 3 times), the three blank spaces are replaced by three ones and again three blank spaces get created on extreme left side. So, basically it is creating 3 blank spaces (or insert 3 zeroes, in case of SLL/SRL) when we shift it either to right or left.

After one time execution of SRL and SLL, it will contain the same number of ones and zeroes. However, the resultant value of the number will change in each case. I understand the above logic and the way SRL and SLL works but does shifting of n bits to left and then to right make sense in any case or is there any situation in real life, when we make use of it?

2) Coming to my second question, I understand that EX with register 0 as first operand doesn't change anything, it will simply execute the target instruction, (MVC in this case). Since, it works on single instruction, the control after executing the MVC will not execute the XC instruction and will be going back to execute the instruction after the EX instruction. Please let me know if my understanding is correct.

Thanks,
Alok
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Wed Aug 03, 2011 2:06 pm
Reply with quote

Yep, I think that is about it. Just a little more work on the EX to understand whether the register is relevant for the "doesn't change anything" and the trickiest bit is understanding the "doesn't change anything". Absolutely correct about the XC.

As to uses of the Shifts, and the use like described, they are many.

Take a -ve binary halfword. Want to make it positive with the same absolute value? Shift left one bit, shift right one bit.
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Wed Aug 03, 2011 2:57 pm
Reply with quote

Quote:
it will contain the same number of ones and zeroes.

it will not....

any shift logical operation will lose the shifted out bits, and the <shifted in> bits will all be zeroes.

the shift logical will disregard the sign and work on 32/64 bits
the shift will will work on 31/63 bits and preserve the sign

shift left is the non arithmetic way for the multiplication by two
shift right is the non arithmetic way for the integer division by two

take the time to meditate on the POP SA22-7832-08
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Wed Aug 03, 2011 7:33 pm
Reply with quote

As enrico is alluding, I've been a little slack with my terminology. Sorry about that.
Back to top
View user's profile Send private message
PeterHolland

Global Moderator


Joined: 27 Oct 2009
Posts: 2481
Location: Netherlands, Amstelveen

PostPosted: Wed Aug 03, 2011 9:13 pm
Reply with quote

Dont use the shift too much else people are going to say :
There are some shifty fellows on this forum. icon_eek.gif

Especially when you shift leftish.
Back to top
View user's profile Send private message
alokagarwaljgd

New User


Joined: 02 Jun 2006
Posts: 28

PostPosted: Thu Aug 04, 2011 11:12 am
Reply with quote

Quote:


Quote:

it will contain the same number of ones and zeroes.


it will not....

any shift logical operation will lose the shifted out bits, and the <shifted in> bits will all be zeroes.


What I meant by saying "After one time execution of SRL and SLL, it will contain the same number of ones and zeroes. However, the resultant value of the number will change in each case." is that if we have R2 = 0111 and if we execute the following instructions

1) SLL 2,1
After execution, R2 = 1110, which contains 3 ones and 1 zero
Then if we execute the below instruction

2) SRL 2,1
After execution, R2 = 0111, which again contains 3 ones and 1 zero

Hence, in each of the above case, we can see that after one time execution of SRL and SLL, they have same number of ones and zero
i.e. 3 and 1 respectively.

I am aware of the use of shift instruction for the purpose of multiplication and division but what I was seeking was its practical use/purpose with respect to specifically SLL and SRL shifting n bits to left and right side respectively.

Bill,

Thanks for your confirmation with respect to EX question.

I also did as suggested by you for shift instruction. i.e. I had taken a -ve binary halfword for -3 which is
1111 1111 1111 1101

After shifting left one bit, we have
1111 1111 1111 1010

Then after shifting the above resultant to right by one bit, we have
0111 1111 1111 1101

but this obtained value is not equal to the positive value that we want i.e. 3

Can you please help here, is there anything wrong in my calculations?

Peter,

I am sorry, I didn't get what you meant. Can you please explain?

Thanks,
Alok
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Thu Aug 04, 2011 11:47 am
Reply with quote

Alok,

Because I was unthinking, I'm sorry. I was struggling for an example of why you'd you SLL followed by SRL and thought "aha, clobber the sign", without thinking that you'd end up with a huge positive bearing no useful relationship to the original number. My mistake.

Peter's comment was just a word-play joke, nothing to do with anything.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Thu Aug 04, 2011 12:18 pm
Reply with quote

enrico already explained that there is an important difference between the shift bits logical and shift bits instructions.

attention to detail......
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Thu Aug 04, 2011 7:20 pm
Reply with quote

just plain stubborness..
Your example of not losing 0/1 s is a specific case, not the general one

and ... I forgot to clarify a shift right ( arithmetical shift right will propagate the sign
(remember negative numbers are in 2s complement )

no shifting will change the sign, read about LNR
again before posting Your wrong conclusions read for the umpteen time the manual

to save some of Your precious time here is the link for the download
www-304.ibm.com/support/docview.wss?uid=isg2b9de5f05a9d57819852571c500428f9a
Back to top
View user's profile Send private message
alokagarwaljgd

New User


Joined: 02 Jun 2006
Posts: 28

PostPosted: Fri Aug 05, 2011 12:19 am
Reply with quote

Enrico,

I never said that shifting to left and right will not lose 0/1. It will definitely do that and in my example also it is doing that.

Let me give another example, say
if we have R2 = 1110 and if we execute the following instructions

SLL 2,1
After execution, R2 = 1100, which contains 2 ones and 2 zeros
Then if we execute the below instruction

SRL 2,1
After execution, R2 = 0110, which again contains 2 ones and 2 zeros

In the above example, shifting to left resulted in loss of 1 and introduction of 0 at end and then shifting to right resulted in loss of 0 at extreme right and introduction of 0 in the beginning.
Hence, in each of the above case, we can see that after the execution of SRL and SLL, they have same number of ones and zero
i.e. 2 and 2 respectively.

I think there was some confusion here, when I am saying "After one time execution of SRL and SLL, it will contain the same number of ones and zeroes" I am not comparing the number of 0/1 s produced in the register after execution with the first initial value of the register( 3 ones and 1 zero in this example). I am only comparing the number of 0/1 s produced in the register (R2 = 1100, which contains 2 ones and 2 zeros, in this example) after once SLL executes with that of number of 0/1 s produced in the register (R2 = 0110, which again contains 2 ones and 2 zeros, in this example) after a successive SRL executes.

I also understand the sign propagation in case of arithmetic shifts. But, here my main focus from the beginning was logical shifts (n bit shifts of SLL followed by n bit shifts of SRL). And since, Bill suggested me to take a -ve binary halfword and try out the shifts. I did the same as suggested by him to understand if that can act as a practical use/purpose of shifting n bits to left using SLL and then immediately followed by SRL for shifting n bits to right.

Thanks,
Alok
Back to top
View user's profile Send private message
Ed Goodman

Active Member


Joined: 08 Jun 2011
Posts: 556
Location: USA

PostPosted: Fri Aug 05, 2011 8:03 pm
Reply with quote

good use for bit shifting: reading/writing flag bits.

flag byte has 10010001

do this loop
right-most bit = AND with 00000001
shift right
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> Mainframe Interview Questions

 


Similar Topics
Topic Forum Replies
No new posts Shift left VB record without x00 endi... DFSORT/ICETOOL 11
No new posts Error 0C1 Reason Code 1 with branch i... PL/I & Assembler 3
No new posts technology shift from Mainfame to AS400 All Other Mainframe Topics 3
No new posts Overlay with right shift DFSORT/ICETOOL 3
This topic is locked: you cannot edit posts or make replies. BBC is seeking a Security Operations ... Mainframe Jobs 0
Search our Forums:

Back to Top