• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help! Can you explain these bitwise operations?  RSS feed

 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NOTE: I'm also asking in this forum: http://forums.sun.com/thread.jspa?threadID=5343597&tstart=0 but do not yet have an answer.

I am working with a file that has a header with the size of the file encoded into 4 bytes like:

The ID3v2 tag size is encoded with four bytes where the most significant bit (bit 7) is set to zero in every byte, making a total of 28 bits. The zeroed bits are ignored, so a 257 bytes long tag is represented as $00 00 02 01.



I have found someone's code that puts this together into the integer value, but i don't understand why they do each step. Is there anyone here who can explain this code to me?


[ October 29, 2008: Message edited by: Dan Bizman ]
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it looks like you're of the opinion that posting in more than one forum is fine.

http://forums.sun.com/thread.jspa?messageID=10485049

Perhaps you posted your question in yet another forum and already got answered there. Since you didn't post a link is this thread to all of your other posts, I cannot check to see if you were answered in one of your other threads. So by answering you here I might be wasting my time. So, I think I'll pass.

Best of luck though!
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Piet Verdriet:
Well, it looks like you're of the opinion that posting in more than one forum is fine.

http://forums.sun.com/thread.jspa?messageID=10485049

Perhaps you posted your question in yet another forum and already got answered there. Since you didn't post a link is this thread to all of your other posts, I cannot check to see if you were answered in one of your other threads. So by answering you here I might be wasting my time. So, I think I'll pass.

Best of luck though!


I really don't understand that. How am I to know that you frequent both forums? What if you only post here and in the other forum, no one ever answers my question? Without posting here as well, I'd never find someone who knows the answer.

Once I got the answer in the other forum, I would have posted the answer here as well and told people not to help anymore.

Seriously though. how can i know people will check both forums? Or, are you saying it's ok if I put a link to the other post in this one?
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got the answer on another forum:
the original format uses 7 bits out of each byte. To make that into a 32 bit (actually 32-bit) number, it is first converted to a set of 8-bit bytes.

Bytes[3] is the lowest byte, so it holds 7 bits from tagsize[3] and 1 bit from tagsize[2].
Bytes[2] is the second lowest byte, so it holds the remaining 6 bits form tagsize[2], and 2 bits from tagsize[1].
Bytes[1] is the reamining bits of tagsize[1] and part of tagsize[0]
Bytes[0] is the last bits of the tagsize[0].

The number of bits correspond to the masks used in the & operation, for example 1 bit -> & 1, 2 bits -> & 3 and 6 bits -> & 63

Once we have the bytes values, we can then shuffle it all into a 32-bit integer. As each byte is 8 bits, we need to shift by 0, 8, 16 and 24 bits to form the 32-bit number.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dan Bizman:
Or, are you saying it's ok if I put a link to the other post in this one?

Well, here's the explanation from our FAQ: CarefullyChooseOneForum. Officially we don't mind if you cross-post your questions in other forums, because the other forums are none of our business. Although we do ask that you tell people that you are cross-posting.

However (as that FAQ entry I linked to says), people are likely to take exception to it. We don't object to them doing that either, as long as they do it nicely.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:

Well, here's the explanation from our FAQ: CarefullyChooseOneForum. Officially we don't mind if you cross-post your questions in other forums, because the other forums are none of our business. Although we do ask that you tell people that you are cross-posting.

However (as that FAQ entry I linked to says), people are likely to take exception to it. We don't object to them doing that either, as long as they do it nicely.


I think that link applies to posting in multiple JavaRanch forums.
BeForthrightWhenCrossPostingToOtherSites applies to posting on other sites and I think Dan followed the recommendations in there.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!