• Post Reply Bookmark Topic Watch Topic
  • New Topic

and and or operator help  RSS feed

 
Donald Gooden
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im trying to understand what ivor is saying about the and, and or operators and the mask.
if i understand it correctly the & operator prevents you from changing a bit that is one when a mask is involved and changes all others to 0
and the | operator forces a bit to 1 when the mask is 1.

my question is when would i need to actually use the & ,| operators ?when will i need to manipulate the bits in a variable?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Was this meant to be a reply to another topic?

Donald Gooden wrote:my question is when would i need to actually use the & ,| operators ?when will i need to manipulate the bits in a variable?

Yes. Which in my experience is a very rare occasion.
 
Donald Gooden
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no it was meant to be a new thread
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should have said Ivor is the author of the book you are using, otherwise who will know?
Find out the constants in the Font class which mean bold, italic, etc. You may find they are powers of 2, like 1 2 4 8 etc. That means they have one 1 bit and 31 0 bits. That means you can use the bitwise or operator to put the two together, so 2 | 1 will have two bits set, making 3. If the bit has already been set in the font attributes, repeated use of the or operator won't change it.
You can toggle a bit with the exclusive or operator.
You can find whether a bit has been set with the and operator
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to Google for myself, because the Ranch search seems to be misbehaving. I found this which will probably help you.
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only cases I've used them were when I was working with file formats or protocols or hardware, where data is put in as tiny a package as possible.

A concrete example is that I made an editor for an old game I enjoyed. Characters could move around in areas. These areas had a couple of binary properties, for instance, whether they were indoor or outdoor, whether it could rain/snow or not, and a few others. You only need a bit of information to store each particular property, but most machines can't work with anything smaller than a byte.

To add the properties, you can use the | operator:
To check or remove properties, you can use the & operator:
 
Donald Gooden
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i thought i stated it was ivors book my apologies my hands seemed to move slower then my brain

are you saying the fonts have one 1bit and 31 0's ? and the & and | operator can be used to manipulate these bits?
im trying to get a firm grasp on the arithmetic operators

a better understanding of
floating point arithmetic
op= operators
and the cast and explicit cast any suggestions or explanations from the community would be grateful.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some basic illustration on the bitwise operators are as follows:

The & operator states that each bit in the values are compared where:
1 & 1 = 1
1 & 0 = 0
0 & 0 = 0


The | operator states that each bit in the values are compared where:
1 | 1 = 1
1 | 0 = 1
0 | 0 = 0


The ^ operator states that each bit in the values are compared where:
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 0 = 0
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!