• Post Reply Bookmark Topic Watch Topic
  • New Topic

bitwise operators  RSS feed

 
Bob Rosenblum
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to find out how to set a bit and then inquire as to whether a bit is set.

I would like to show the before and after with a System.out.println();

I believe I have to use the | operator and the shift right to set it and the & to inquire against it, but am not sure exactly how to do it?

Can anyone help me?

bob
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I remember the "good old days" when most developers knew assembly -- and did this a lot. Now with modern programming languages and the Object Oriented paradigm, developers are lucky to even use the bitwise operators, much less know how to set, clear, and detect specific bits in an int.


I suggest you google for "bit masks". Try to find a tutorial that explains them, what they are for, how to create them, and how to use them. What you are trying to do with the shifting is to create a bit mask, and the ORing and ANDing are how you use the masks to set, clear, and detects specific bits.

Henry
 
Bob Rosenblum
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I understand it, if you define a variable as a byte, then by using the | operator and the >>> operator, you can update an individual bit in the designated byte.

For example, in a program where b is defined as an array of bytes, you can do the following:



which will move a 1 in the first bit (bit 0) of byte 0.

if i do a it does show a value of 128, indicating that the 0 bit was indeed set.

However, I need to not only set various bits in random bytes, I have to go back and interrogate the individual bits later to see if they are set.

I think, in some way, you use the & operator, but not sure how.

My questions are:
1. Did I set the bit properly?
2. How do I determine later if the bit was saved? I can't simply check the value of the byte, because if the first and last bit was set to 1, then the value would be 129. I need to write some kind of loop where I check each bit and interrogate it.

thanks

bob
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I just did a google (of "bit masks" as mentioned in my previous post). The first two hits gave full details, that answers your questions.

Doing a google, and some reading, may be faster than just waiting for a response here.

Henry
 
Bob Rosenblum
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did the same, but didn't find anything that I could understand. It would seem that it should only take a single line of code to interrogate a bit and print the results. That is all that I am looking for

bob
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Rosenblum wrote:I did the same, but didn't find anything that I could understand. It would seem that it should only take a single line of code to interrogate a bit and print the results. That is all that I am looking for

bob


I find it hard to believe that you would rather memorize a "single line of code" than to spend more time to try to learn the concept... but I guess it is your decision.

Henry
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println("No 3 bit = " + ((i & 8) == 0 ? 0 : 1));

where i is an int. You can probably do the same with any integer, ie byte, char, short and long too, but I would suggest you try it. Here I am using 8 as a mask for the no 3 bit where the least-significant bit is no 0. I suspect you need the third () pair because & has a lower precedence than ==, but that is worth checking.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!