• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Byte Packing/Unpacking

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so I've been trying to figure out bitwise operators and such, and have gone through examples and tutorials, and I can't seem to grasp it because none of them seem to explain what their goal is, so I've come up with a hypothetical, and would appreciate it if someone could show me an example

Say I have 8 values that are true or false and I want to store these in a single byte, send it across a network. Then, unpack it and use it to toggle 8 different things. (I don't need help the network part, just the packing and unpacking)

For example you have:
byte[] vals = { 1, 0 , 1, 0, 1, 0, 1, 1 };

How would you pack vals into a single byte, then unpack it and get a byte array?

Thanks in advance.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Carr wrote:Ok, so I've been trying to figure out bitwise operators and such, and have gone through examples and tutorials, and I can't seem to grasp it because none of them seem to explain what their goal is, so I've come up with a hypothetical, and would appreciate it if someone could show me an example

Say I have 8 values that are true or false and I want to store these in a single byte, send it across a network. Then, unpack it and use it to toggle 8 different things. (I don't need help the network part, just the packing and unpacking)

For example you have:
byte[] vals = { 1, 0 , 1, 0, 1, 0, 1, 1 };

How would you pack vals into a single byte, then unpack it and get a byte array?

Thanks in advance.



Not sure how this is going to help you. You went through examples and tutorials but didn't get it. And is now expecting to get it by having a specific example.

Would it not be better to review the tutorial examples, explain what you think is happening, and why it doesn't make sense -- and maybe we can set you in the right direction?

Henry
 
John Carr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Not sure how this is going to help you. You went through examples and tutorials but didn't get it. And is now expecting to get it by having a specific example.

Would it not be better to review the tutorial examples, explain what you think is happening, and why it doesn't make sense -- and maybe we can set you in the right direction?

Henry


Because I learn best via example, and every example I've seen has nothing to do with anything. They're just rubbish.

Like, I understand the shifts, it's just the bitwise operators. If I where able to actually see what the bits where it would be easier for me, but I haven't found a way to do that without continually converting the bytes value to binary via a website.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Carr wrote:Because I learn best via example, and every example I've seen has nothing to do with anything. They're just rubbish.



And we can't help you, if we don't know what you are confused about.

What happens if we give you another example, and you don't understand it? Is it to be dismissed as rubbish too?

Henry
 
John Carr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:And we can't help you, if we don't know what you are confused about.

What happens if we give you another example, and you don't understand it? Is it to be dismissed as rubbish too?

Henry


I'm confused about bitwise operations and modifying values in a primitive data type.

I don't dismiss examples as rubbish just because I don't understand them, they are rubbish because they are not explained well, nor do they have a practical application.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Carr wrote:I'm confused about bitwise operations and modifying values in a primitive data type.



Then this is a good place to start. What are you confused about? Gives us an example, and explain to us what is confusing. Asking for code example, for a specific example, is no different than a tutorial example. If you don't believe me, here is the answer to your question....



John Carr wrote:
Say I have 8 values that are true or false and I want to store these in a single byte, send it across a network. Then, unpack it and use it to toggle 8 different things. (I don't need help the network part, just the packing and unpacking)

For example you have:
byte[] vals = { 1, 0 , 1, 0, 1, 0, 1, 1 };

How would you pack vals into a single byte, then unpack it and get a byte array?





Henry
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John, let me help you as per how I have understood your difficulty.

So you want to transfer some bits from one place to other and at the other place you want to
separate those bits so that you can use each bit and do some meaningful work out of it.

So suppose you have a byte say
byte signal = 10101011

you want to transfer this and get the digits seperated i.e. 1, 0 , 1, 0, 1, 0, 1, 1

So do one thing

when you get signal at the other end, apply this algorithm

signal MOD 2 - > gives you last digit i.e. 1
signal DIV2 MOD 2 - > gives you last digit i.e. 1
signal DIV2 DIV2 MOD 2 - > gives you last digit i.e. 0

--- and so on.
 
John Carr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry, that actually helped me a lot.

I wrote an unpacker based on the packer, here is the code:

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic