Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java & Least Significant Bits  RSS feed

 
Simon Ritchie
Ranch Hand
Posts: 105
4
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

I'm working on an application that needs to be able to manipulate the values of different bits in a byte.  For example, I need to be able to change the value of the least significant bit in a given byte from 0 to 1 or vice versa.  I've been doing some Googling and some searching on here regarding bitwise operators and changing the values of least significant bits but I'm afraid I don't really understand how to do it.  Are there any basic examples anyone can provide me with or any handy primers?

Thanks
 
Liutauras Vilda
Marshal
Posts: 4260
256
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say you are working on application, could you please show us your progress, so somebody could help you.
 
Liutauras Vilda
Marshal
Posts: 4260
256
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also please give us some more context, as i think there is too little information about your actual goals.
 
Simon Ritchie
Ranch Hand
Posts: 105
4
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Liutaras,

Apologies for the lack of clarity in my original post.  I would post the source code of my application but it is quite large and only a small part of it involves changing the least significant bit of a byte.  Here's an example of what I need to be able to do.

Let's say I have a byte variable that I initialise to zero



My assumption is that the above code creates a byte in memory with the following bits



I need code that will invert the value of any bit of the byte that I choose.  So if I wanted to change the value of the least significant bit:



Hopefully I've explained it better.
 
Liutauras Vilda
Marshal
Posts: 4260
256
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the one you showed us is most significant, meaning, the one which has highest value.

Left is most significant, right is least significant.

Please clarify that point.
 
Simon Ritchie
Ranch Hand
Posts: 105
4
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My mistake, I mean the most significant bit (bit zero).
 
Norm Radder
Rancher
Posts: 2037
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The XOR operator (^) used with a bit of 1 will invert a bit.  Use a shift operator to position the 1 at the bit you want to invert.

Write a small test program that uses the XOR operator and some shift operators and print out the results so you can see what the results of each operations is.
 
Carey Brown
Bartender
Posts: 2697
41
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Position is thought of from right to left. This matches up with the left/right shift operations.
 
Campbell Ritchie
Marshal
Posts: 54882
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Position is thought of from right to left. . . .
Bit Position:       8   7   6   5   4   3   2   1 . . .
I was always taught 0‑based bit positions, so I expected to read
Bit Position: 7 6 5 4 3 2 1 0.

That means that the value of a number where only the nth bit is set is 2 except for the leftmost bit in a signed integer.
 
Liutauras Vilda
Marshal
Posts: 4260
256
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I was always taught 0‑based bit positions, so I expected to read
Bit Position: 7 6 5 4 3 2 1 0.

Right. In case somebody forgot the formula, please remind expressed with 0 index.

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

Also, to be pedantic, Java uses twos complement, so, bit 7 should have a value of "-128"....

Henry
 
Piet Souris
Rancher
Posts: 1914
66
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To set a bit, you can OR it with 1, to clear a bit AND it with 0, to toggle a bit XOR it with 1.
It is slightly more involved than that though. If you have an integer b, and want to clear bit 3, then we must AND b with an integer that has all bits set to 1, except for bit 3. A handy way for this is to use NOT (1 << 3), and so we get: b & ~(1 << 3).

I wrote a little class that can do these bit jugglings, as a training for myself in BinaryOperators,
The way it works is as follows: say we have a byte, short, integer or long, call it b, and we want to set bits 1 and 2, clear bit 3 and toggle bits 4, 5, 6.
We get: BitJuggler.initialize(b).setBits(1, 2).clearBits(3).toggleBits(4, 5, 6).getValue();
I don't know if it is a handy way, but it was fun to write it.


 
Simon Ritchie
Ranch Hand
Posts: 105
4
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a brilliant class, Piet.  I'm not familiar enough with lambda expressions for it to make immediate sense but I have a much clearer idea of how bitwise operations work now.  Thanks very much!
 
Campbell Ritchie
Marshal
Posts: 54882
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by looking at the documentation for LongBinaryOperator. It says it is a Functional Interface (other details in the Java® Language Specification), which means it has one and only one “abstract” method. That method takes two longs as its parameters, as I shou‍ld have guessed from the name of the interface. The first parameter is the left operand of the bitwise operator and the second is the right operand of the left-shift operator. What it means is you are creating an instance of the interface. I sometimes work backwards from an anonymous class to a λ, but this time I shall do it the other way.Now, let's lose the ellipses, and work out what the method body shou‍ld be:-Now, that new part won't compile because it isn't a statement, so let's convert it to a statement:-Now, we get rid of the -> arrow token/goes to operator:-Now, let's add the name of the solitary method and its return type:-Well, the format given as the API link won't compile, but it does tell us what type both parameters are. So we move those types into the () and it will start to look like old‑fashioned code:-Now, let's have the type of the actual object, which is the same as its declared type:-Add the new operator and some () to change the type name into a constructor invocation:-A pair of {} to delineate the class body and a ; to terminate the initialisation, improve the indentation, and Bob's your uncle:-The () around the left-shift operator in lines 7 and 9 are redundant, but they are needed in line 8.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!