On Friday 21 April 2006 10:15, Smith, Derek wrote: > -----Original Message----- > From: Smith, Derek > Sent: Friday, April 21, 2006 10:22 AM > To: Perl Beginners > Subject: RE: Extracting a bit of a scalar variable > > > ----Original Message----- > From: Dr.Ruud [mailto:[EMAIL PROTECTED] > Sent: Friday, April 21, 2006 9:19 AM > To: beginners@perl.org > Subject: Re: Extracting a bit of a scalar variable > > Praveena Vittal schreef: > > I am newbie to PERL. > > ITYM: Perl. > > > I think this is a very simple question to ask. > > Well, let's see. > > > Is there any way to print a particular bit of a digit after converting > > to binary form. > > Why convert anything "to binary" first, if you can use > "$var & (1 << $bitpos)"? > > > $ echo 200 > > | perl -nle 'printf "\n b %5d\n-- -----\n", $_; > > for $b (0 .. 15) > { printf "%2d %5s\n", > $b, $_ & ($p = 1 << $b) ? $p : "" }' > > > snip > -- > Affijn, Ruud > > ************************************************************** > > b 200 > -- ----- > 0 > 1 > 2 > 3 8 > 4 > 5 > 6 64 > 7 128 > 8 > 9 > 10 > 11 > 12 > 13 > 14 > 15 > > So what does this output tell me in English, that the decimal number 200 > is composed of ???
It tells me that in the binary representation of the decimal number 200, bits 4, 7, and 8 are set. It even makes it easy for me to check by supplying me with the decimal values of at those positions. Decimal: 200 Binary: 0000_0000_1100_1000 Hex: 00C8 128+64 +8 = 200 > > > Was the above a silly question? I have not seen a reponse. I do > understand what this "$var & (1 << $bitpos)"? is doing, nor do I > understand what the output means? > Bit Manipulation perldoc perlop > Please explain, > > Thx > Derek > > > Working together. For life.(sm) > _________________________________________________ > > This message is for the designated recipient only and may contain > privileged, proprietary, or otherwise private information. If you have > received it in error, please notify the sender immediately and delete the > original. Any other use of the email by you is prohibited. > > Dansk - Deutsch - Espanol - Francais - Italiano - Japanese - Nederlands - > Norsk - Portuguese - Svenska: www.cardinalhealth.com/legal/email -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>