• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java & Least Significant Bits  RSS feed

 
Simon Ritchie
Ranch Hand
Posts: 104
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: 3961
214
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: 3961
214
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: 104
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: 3961
214
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: 104
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: 1772
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: 2608
40
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
Sheriff
Posts: 54067
130
  • 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: 3961
214
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: 23028
120
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: 1843
61
  • 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.


 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!