I have asked this question before, but i think i could not totally understand the diffefence between bitwise and logical operands. According to the book i am using to learn java, the bitwise operands can change the value and this is described as side effect. Also, in bitwise operands, both of the sides are evaluated. that means they don't perform short-circuit evaluation. For example,

    (gender == 1) & (age >= 65) 
    //evaluates age 65 >= regardless of whether gender is equal to 1. 

up to here, i understand what it is. However, aafter this point the book gives another example for bitwise or (|).

(birthday == true) | (++age>= 65) 
// here, age always increases by 1 regardless whether the first condition is true or not. 

however, i did not understand what it means by >= 65. Does it say that, if the age is bigger than 65, then the expression will be true or something like that ?

Note: Maybe i could not explain it properly, but all i want to learn is where and how to use both of them. Can you please explain ?

Recommended Answers

All 4 Replies

Yes it means just that, and because the increment is pre-increment, it means same time and that age was 64 or more before if the condition was true. And regardless the age will allways be increased by one.

If consider the meaning of naming of variables, it looks likely that operator should have been and (&& only would work, not bitwise & here) to mean "if it is birthday, increase age and see if the new age is equal or over 65"

Yes it means just that, and because the increment is pre-increment, it means same time and that age was 64 or more before if the condition was true.

I could not understand what you mean by pre-increment, what if the statement was &. Then if it was nto the birthday, the age would not increase by 1. ( also age is supposed to be more or equal to 65. Right? ) Also, we cannot change any value with logical operands, can we ?

pre increment is done before actual execution in surrounding statement, it is done by putting the two plusses before the statement. If the operation is bitwise, the increment happens in every case and age is allways increased by one. But here with shortcutting kind logical operators, the value of age would only happen if birthday is set:

    if (birthday && ++age > 65)

Notice that I took out == true it is in my taste poor programming style as birthday is supposed to be allready boolean type. Here age would be not changed if birthday is zero value like false.

pre increment is done before actual execution in surrounding statement, it is done by putting the two plusses before the statement. If the operation is bitwise, the increment happens in every case and age is allways increased by one. But here with shortcutting kind logical operators, the value of age would only happen if birthday is set:

thanks a lot. I think i got the idea of it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.