How do I parse certain "bits" out of bytes?

J

Joe Smith

Adam said:
Now, what do you
mean when you say "this could be only a problem if there are more than
8 bits"? Because in my real-world example, I need to get data from 32
bits. Can I not use these same techniques?

If you have an 8-bit number and shift it right 3 bits, then you have
a 5-bit quantity. If all you are interested in is those 5 bits,
then fine.

If you have a 32-bit number and shift it right 3 bits, then you have
a 29-bit quantity. If all you are interested in is 5 bits, then you
will have to perform an extra step to ensure that you are getting only
5 bits. That's where the 'AND' operation comes in.

-Joe
 
J

Joe Smith

Adam said:
I still have no idea how to extract
a few bits out of a longer string of bytes. The help files all seem to
have been written with the assumption that I majored in computer
science and learned how to program years ago in some pre-Perl language

Whenever dealing with bits or bytes, it is assumed that the student
has been exposed to Computer Science 101 where they teach you about
the basics: numbering systems, bit positions, AND/OR/XOR. Almost
any tutorial on binary numbers will provide you with the nomenclature
and concepts you need to know.

You are somewhat correct in describing it as "some pre-Perl language"
in that the concepts existed before Perl. But operations with binary
bits is not "programming from years ago"; it is also part of programming
in modern languages.
-Joe
 
D

Damian James

...
The people in this newsgroup are acting as if I were attacking them
personally; my minor complaint is with the documentation, which makes
the assumption that the reader is already familiar with a lot of
technical programming terms, and furthermore has learned other ^^^^^^^^^^^^^^^^^^^^^^^^^^^
languages before perl. (There is indeed good "beginner's"
documentation for perl, but it never really gets into binary data;
neither "Learning Perl" nor the "Perl Cookbook" were able to help.)

That's the issue, really. When you talk about binary manipulating
binary numbers, you are talking about binary arithmetic. Shifting bits,
bitwise AND and OR are fundamental operations, like addition or division.

So when you start using perl to manipulate binary data, knowing what it is
that you want to do with the data is not in the domain of the specific
programming language you are using. It is not the province of the perl
documentation to teach you how to do that, any more that you should expect
the perl documentation to teach you about addition and division.

Many of the folks here have made suggestions in keeping with knowing this.
None of the regulars here are people who would intend, in their responses,
merely to insult you. Most folks here would insist that you know something
about your problem domain before attempting to write a program that addresses
it, though.

Hope this makes sense to you.

-damian
 
F

Fabian Pilkowski

* Mike Heins said:
Here is a way that doesn't rely on the somewhat impenetrable
unpack() function:

my $value = 0x65;
my $binstring = sprintf '%b', $value;
$binstring = '0' . $binstring while length($binstring) < 8;

Sure, anyone could understand what you're doing here but it's more
elegant to insert "08" into the format string

my $binstring = sprintf '%08b', $value;

instead of a while loop.

regards,
fabian
 
P

Paul

Fabian said:
Sure, anyone could understand what you're doing here but it's more
elegant to insert "08" into the format string

my $binstring = sprintf '%08b', $value;

instead of a while loop.

regards,
fabian
The idea of "bit bashing" seems to have died from the early days of
computing whhere memory was a real problem and programmers had to
operate down at the lowest levels of the machine.

It is probably still needed in embedded systems for monitoring
industrial control inputs but is most likely supported by "bit bashing"
libraries which hide the underlying data from view.

My suggestion to the person who started this thread is to do a quick bit
of home study on binary numbers and get to understand how they work ( it
aint hard ) !!

Start with the ASCII chart and then work your way up to numerical values
- maybe write your own "bit bashing" library.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top