posted 3 years ago

Hello all I am using Ivor Hortons beginning java 7th addition and I am having a hec of a time understanding the the bit wise operators, casting, op operators use,shift operators, just the whole math section of chapter two past the basic math operators. Can anyone recommend some supplemental material so that i can understand these functions much better. He explains the functions gives examples and try out code that should put it together but I'm having trouble putting it together. At the end of chapter 2 there are four exercises and I believe I can only do 1. I really want to get my associates certs by oct. so I'm using Ivor's book to get a foundation and then start doing core java vol one and two for the rest, and then the study guide for the cert test. I would like to have my professionals certs by the end of the year but I must first make it through this part, s.os from a determined but struggling green horn thanks .

posted 3 years ago

I'm not sure how your book tackles it, but when I've tried to explain it, I've always found it best to start with a single bit (0/1) and the basic operations ('&', '|', '^' and '~') first, before you move on to whole words and bit shifting.

First: it's worth remembering the

& = 'AND'

| = 'OR'

^ = 'Exclusive OR'

~ = 'NOT'

Second: For a single bit:

0 = false

1 = true

So, given that, when you are given something like '

true AND false

What would you expect the result of that to be? Now convert it back to a

And '

NOT true

What would you expect the result of that to be?

'^' is the trickiest one because it's not something we normally find in speech, but the easiest way I find to remember it is to rename it:

'^' = 'Is

and use the same technique as above.

So: what will the result of '

After that, it's simply learning how they operate on groups of bits (which is all an

'~' (NOT) is simply applied to All the others are applied to There's a bit more to know - especially things like numeric promotion - but while you're trying to get your head around it, I suggest you tackle one thing at a time. And the easiest way to do that is to assume that both numbers (and the result) are the

I suspect also that what you really want to know is

And you can do some amazing things with them. Just one quick example:

'^' means "is different to"; therefore, x ^ x

And that's

These days, they're more of a curio than anything, and it's unlikely that you'll need to use them a lot when you program; but for certain low-level operations, they're still the fastest way to go.

As for shifting: My advice - get your head around those four operators above first, and make sure you really understand them;

Hope it helps.

Winston

- 1

Donald Gooden wrote:Hello all I am using Ivor Hortons beginning java 7th addition and I am having a hec of a time understanding the the bit wise operators, casting, op operators use,shift operators, just the whole math section of chapter two past the basic math operators.

I'm not sure how your book tackles it, but when I've tried to explain it, I've always found it best to start with a single bit (0/1) and the basic operations ('&', '|', '^' and '~') first, before you move on to whole words and bit shifting.

First: it's worth remembering the

*name*of the operator:

& = 'AND'

| = 'OR'

^ = 'Exclusive OR'

~ = 'NOT'

Second: For a single bit:

0 = false

1 = true

So, given that, when you are given something like '

`1 & 0`', just write it out in longhand:

true AND false

What would you expect the result of that to be? Now convert it back to a

*single bit value*(1 or 0).

And '

`~1`' ('~' only takes a single argument) becomes:

NOT true

What would you expect the result of that to be?

'^' is the trickiest one because it's not something we normally find in speech, but the easiest way I find to remember it is to rename it:

'^' = 'Is

__different__to'

and use the same technique as above.

So: what will the result of '

`1 ^ 1`' be? Again, write it out in longhand...

After that, it's simply learning how they operate on groups of bits (which is all an

`int`, or a

`byte`really are):

*all*the bits in the number.

*each pair of bits*in the two numbers - ie, the 1st bit of x is paired with the 1st bit of y, the 2nd with the 2nd, and so on, and the operator is applied to

*each pairing*to produce a result.

*same type*.

I suspect also that what you really want to know is

*why*they exist, and a lot of it is historical. They were the first real instructions computers had, because they were the result of applying current (or voltage) across a single transistor. And even now, in the days of chips, they are

*blisteringly*fast - often only a single cycle of your CPU; which these days are measured in

*giga*hertz.

And you can do some amazing things with them. Just one quick example:

'^' means "is different to"; therefore, x ^ x

*must*equal 0 (false). So, if you have a pile of memory that you need to set to 0 - a very common requirement - just divide it into

`byte`s (or

`int`s, or

`long`s) and '^' each one

*with itself*.

And that's

*exactly*how computers used to do this (not sure about now), because it was by far the quickest way.

These days, they're more of a curio than anything, and it's unlikely that you'll need to use them a lot when you program; but for certain low-level operations, they're still the fastest way to go.

As for shifting: My advice - get your head around those four operators above first, and make sure you really understand them;

*then*tackle shifting. One thing at a time.

Hope it helps.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here