Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Java & Least Significant Bits

Ranch Hand
Posts: 156
13
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

Marshal
Posts: 5634
387

Liutauras Vilda
Marshal
Posts: 5634
387
Also please give us some more context, as i think there is too little information about your actual goals.

Simon Ritchie
Ranch Hand
Posts: 156
13
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: 5634
387
But the one you showed us is most significant, meaning, the one which has highest value.

Left is most significant, right is least significant.

Simon Ritchie
Ranch Hand
Posts: 156
13
My mistake, I mean the most significant bit (bit zero).

Rancher
Posts: 2954
32
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.

Saloon Keeper
Posts: 4104
48
Position is thought of from right to left. This matches up with the left/right shift operations.

Marshal
Posts: 58829
179

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: 5634
387

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.

author
Sheriff
Posts: 23484
138

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

Henry

Master Rancher
Posts: 2540
87
• 1
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: 156
13
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: 58829
179
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.

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?