# Shifting - hex digits

Uma Viswanathan

Ranch Hand

Posts: 126

posted 15 years ago

Could someone confirm whether my understanding is correct (step by step)?

From the mockup exam, http://joppa.appliedreasoning.com/javaCert/html/exam.html

Ques No: 23

(byte) 0x81 >> 2;

Steps:

1)

a) Each hex digit must be represented in four binary digits (zeros and ones) and each octal digit must be represented in three binary diigts. So, when we represent 8 in four binary digits, we get 1000. Similarly 1 is equivalent to 0001.

So, the binary representation of 0x81 is

0x 0000 0000 0000 0000 0000 0000 1000 0001

2) Since cast operator has higher precedene than shift operator,

(byte) 0x81 is now

0x 1000 0001 (truncated to byte)

3)

a) Now, when we apply shift operator, L.H.S should be promoted to int( if it is short, char or byte).

b) Since sign bit is 1, we need to extend the byte to int with bit 1. So,

promotion to int gives us

1111 1111 1111 1111 1111 1111 1000 0001

c) now shifting to two bits right, we get

1111 1111 1111 1111 1111 1111 1110 0000 (since the sign bit is 1, we use 1 bit to fill up the gap produced by shift)

So, the result is 0xFFFFFFE0

( I saw the answer is correct, but i want to know whether the logic what i am thinking is correct...)

**********************************************************

Next question from the same mock up exam (Ques no: 24)

0x81 >> 2

As step 1 (above): we get

0000 0000 0000 0000 0000 0000 1000 0001

now, shifting 2 bits right gives us

0000 0000 0000 0000 0000 0000 0010 0000 (here the sign bit is 0,so we use 0 bit to fill the gap)

So, the result is 0x20

Right?

******************************************************

Next question from the same mock up exam (Ques no: 25)

0x1 << 36

Here the L.H.S is int. So, we need to reduce the right hand operand to modulo 32, which gives us

0x1 << 4

Now, 0000 0000 0000 0000 0000 0000 0001 << 4 gives us

0000 0000 0000 0000 0000 0000 0001 0000 (for left shift, we need the gap with zeros)

So, the result is 0x10

If i understand wrongly, then please do correct me?

From the mockup exam, http://joppa.appliedreasoning.com/javaCert/html/exam.html

Ques No: 23

(byte) 0x81 >> 2;

Steps:

1)

a) Each hex digit must be represented in four binary digits (zeros and ones) and each octal digit must be represented in three binary diigts. So, when we represent 8 in four binary digits, we get 1000. Similarly 1 is equivalent to 0001.

So, the binary representation of 0x81 is

0x 0000 0000 0000 0000 0000 0000 1000 0001

2) Since cast operator has higher precedene than shift operator,

(byte) 0x81 is now

0x 1000 0001 (truncated to byte)

3)

a) Now, when we apply shift operator, L.H.S should be promoted to int( if it is short, char or byte).

b) Since sign bit is 1, we need to extend the byte to int with bit 1. So,

promotion to int gives us

1111 1111 1111 1111 1111 1111 1000 0001

c) now shifting to two bits right, we get

1111 1111 1111 1111 1111 1111 1110 0000 (since the sign bit is 1, we use 1 bit to fill up the gap produced by shift)

So, the result is 0xFFFFFFE0

( I saw the answer is correct, but i want to know whether the logic what i am thinking is correct...)

**********************************************************

Next question from the same mock up exam (Ques no: 24)

0x81 >> 2

As step 1 (above): we get

0000 0000 0000 0000 0000 0000 1000 0001

now, shifting 2 bits right gives us

0000 0000 0000 0000 0000 0000 0010 0000 (here the sign bit is 0,so we use 0 bit to fill the gap)

So, the result is 0x20

Right?

******************************************************

Next question from the same mock up exam (Ques no: 25)

0x1 << 36

Here the L.H.S is int. So, we need to reduce the right hand operand to modulo 32, which gives us

0x1 << 4

Now, 0000 0000 0000 0000 0000 0000 0001 << 4 gives us

0000 0000 0000 0000 0000 0000 0001 0000 (for left shift, we need the gap with zeros)

So, the result is 0x10

If i understand wrongly, then please do correct me?

Valentin Crettaz

Gold Digger

Sheriff

Sheriff

Posts: 7610

posted 15 years ago

Everything is right

except that the two last answers should be 0x00000020 (not 0x20)

respectively 0x00000010 (not 0x10).

But the way you presented it is not wrong since you can make abstraction of the leading zeroes but it is more comprehensible that way since we are speaking of integers and not bytes...

HIH

------------------

Valentin Crettaz

Sun Certified Programmer for Java 2 Platform

except that the two last answers should be 0x00000020 (not 0x20)

respectively 0x00000010 (not 0x10).

But the way you presented it is not wrong since you can make abstraction of the leading zeroes but it is more comprehensible that way since we are speaking of integers and not bytes...

HIH

------------------

Valentin Crettaz

Sun Certified Programmer for Java 2 Platform

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML

[Blog] [Blogroll] [My Reviews] My Linked In

Uma Viswanathan

Ranch Hand

Posts: 126

posted 15 years ago

Thank you very much Valentin.

Could you please answer my doubt with threads...(Topic: Threads - doubt...) http://www.javaranch.com/ubb/Forum24/HTML/013404.html

Also please do compare with the explanation for that exam objective from the tutorial http://www.jdiscuss.com/scjp2/notes/Threads.html (which is from JQPLUS)

Waiting for your reply...

Could you please answer my doubt with threads...(Topic: Threads - doubt...) http://www.javaranch.com/ubb/Forum24/HTML/013404.html

Also please do compare with the explanation for that exam objective from the tutorial http://www.jdiscuss.com/scjp2/notes/Threads.html (which is from JQPLUS)

Waiting for your reply...

Valentin Crettaz

Gold Digger

Sheriff

Sheriff

Posts: 7610

posted 15 years ago

I've made some very simple chart about the Threads lifecycle. it is not very detailed but it may help...

Let me know

http://www.javaranch.com/ubb/Forum24/HTML/012134.html

------------------

Valentin Crettaz

Sun Certified Programmer for Java 2 Platform

Let me know

http://www.javaranch.com/ubb/Forum24/HTML/012134.html

------------------

Valentin Crettaz

Sun Certified Programmer for Java 2 Platform

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML

[Blog] [Blogroll] [My Reviews] My Linked In

Uma Viswanathan

Ranch Hand

Posts: 126

posted 15 years ago

Hi Valentin,

Let us continue this topic under http://www.javaranch.com/ubb/Forum24/HTML/013404.html

Thank you very much...please do see that topic...

Uma

Let us continue this topic under http://www.javaranch.com/ubb/Forum24/HTML/013404.html

Thank you very much...please do see that topic...

Uma