operators
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
could anyone please explain bitwise operator, the right shift and left shift. i am new to java
and can't make anything out of these
and can't make anything out of these
sona<br />SCJP
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi,
To make use of bitwise operators you must think in binary. In Java there are 3 types of shift operators:
1. << (Shift Left) - Shift all bits left n times, filling with zeros.
2. >> (Shift Right) - Shift all bits right n times, filling with the sign bit.
3. >>> (Shift Unsigned Right) - Shift all bits right n times, filling with zeros.
Lets take some examples:
1. Start with i = 12 and perform i << 4:
Start: 0000 0000 0000 0000 0000 0000 0000 1100
Shift Left#1: 0000 0000 0000 0000 0000 0000 0001 1000
Shift Left#2: 0000 0000 0000 0000 0000 0000 0011 0000
Shift Left#3: 0000 0000 0000 0000 0000 0000 0110 0000
Shift Left#4: 0000 0000 0000 0000 0000 0000 1100 0000
Convert Bin to Dec: 1*128 + 1*64 = 192
2. Start with i = -42 and perform i >> 4:
Start: 42 --> 0000 0000 0000 0000 0000 0000 0010 1010
1's Comp: 1111 1111 1111 1111 1111 1111 1101 0101
2's Comp: 1111 1111 1111 1111 1111 1111 1101 0110
-42: 1111 1111 1111 1111 1111 1111 1101 0110
Shift Right#1: 1111 1111 1111 1111 1111 1111 0110 1011
Shift Right#2: 1111 1111 1111 1111 1111 1111 1011 0101
Shift Right#3: 1111 1111 1111 1111 1111 1111 1101 1010
Shift Right#4: 1111 1111 1111 1111 1111 1111 1110 1101
1's Comp: 0000 0000 0000 0000 0000 0000 0000 0010
2's Comp: 0000 0000 0000 0000 0000 0000 0000 0011
Answer: -3
3. Start with i = -42 and perform i >>> 4:
Start: 1111 1111 1111 1111 1111 1111 1101 0110
Shift Right#1: 0111 1111 1111 1111 1111 1111 1110 1011
Shift Right#2: 0011 1111 1111 1111 1111 1111 1111 0101
Shift Right#3: 0001 1111 1111 1111 1111 1111 1111 1010
Shift Right#4: 0000 1111 1111 1111 1111 1111 1111 1101
Answer: 268435453 (take my word on this ...)
Regards,
Manfred.
To make use of bitwise operators you must think in binary. In Java there are 3 types of shift operators:
1. << (Shift Left) - Shift all bits left n times, filling with zeros.
2. >> (Shift Right) - Shift all bits right n times, filling with the sign bit.
3. >>> (Shift Unsigned Right) - Shift all bits right n times, filling with zeros.
Lets take some examples:
1. Start with i = 12 and perform i << 4:
Start: 0000 0000 0000 0000 0000 0000 0000 1100
Shift Left#1: 0000 0000 0000 0000 0000 0000 0001 1000
Shift Left#2: 0000 0000 0000 0000 0000 0000 0011 0000
Shift Left#3: 0000 0000 0000 0000 0000 0000 0110 0000
Shift Left#4: 0000 0000 0000 0000 0000 0000 1100 0000
Convert Bin to Dec: 1*128 + 1*64 = 192
2. Start with i = -42 and perform i >> 4:
Start: 42 --> 0000 0000 0000 0000 0000 0000 0010 1010
1's Comp: 1111 1111 1111 1111 1111 1111 1101 0101
2's Comp: 1111 1111 1111 1111 1111 1111 1101 0110
-42: 1111 1111 1111 1111 1111 1111 1101 0110
Shift Right#1: 1111 1111 1111 1111 1111 1111 0110 1011
Shift Right#2: 1111 1111 1111 1111 1111 1111 1011 0101
Shift Right#3: 1111 1111 1111 1111 1111 1111 1101 1010
Shift Right#4: 1111 1111 1111 1111 1111 1111 1110 1101
1's Comp: 0000 0000 0000 0000 0000 0000 0000 0010
2's Comp: 0000 0000 0000 0000 0000 0000 0000 0011
Answer: -3
3. Start with i = -42 and perform i >>> 4:
Start: 1111 1111 1111 1111 1111 1111 1101 0110
Shift Right#1: 0111 1111 1111 1111 1111 1111 1110 1011
Shift Right#2: 0011 1111 1111 1111 1111 1111 1111 0101
Shift Right#3: 0001 1111 1111 1111 1111 1111 1111 1010
Shift Right#4: 0000 1111 1111 1111 1111 1111 1111 1101
Answer: 268435453 (take my word on this ...)
Regards,
Manfred.
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You can certainly play and learn:
BitShift
and
Cat and Mouse Games with Bits
You will learn a lot about bits and binary stuff, when you're playing and having fun.
Roseanne
BitShift
and
Cat and Mouse Games with Bits
You will learn a lot about bits and binary stuff, when you're playing and having fun.
Roseanne
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
As a "newbie" programmer, I'd like to know an example of when/why you would use bitwise operators. Haven't encountered a need for it yet, but I'm sure there are reasons one would work with that.
Thanks!
Thanks!
sona gold
Ranch Hand
Posts: 234
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
thanks everyone
well i myself don't know where i would use bitwise operator elizabeth
but the thing is it is on the list of sjcp exams
and hence the need
well i myself don't know where i would use bitwise operator elizabeth
but the thing is it is on the list of sjcp exams
and hence the need
sona<br />SCJP
sona gold
Ranch Hand
Posts: 234
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
thanks
but if i understand clearly everytime i have to convert the given number into binary and then find the result accordingly
right
but if i understand clearly everytime i have to convert the given number into binary and then find the result accordingly
right
sona<br />SCJP
| What's that smell? I think this tiny ad may have stepped in something. The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |






