Re: Extracting a bit of a scalar variable
Hello Praveena, I recommend you to use pack and unpack function. I believe you can use this to convert the digit to binary and also extract the digit of your choice from the result. perldoc -f pack type this command at your linux console if any to get the help on pack. Regards Nishanth --- Praveena Vittal [EMAIL PROTECTED] wrote: Hi All, I am newbie to PERL.I think this is a very simple question to ask. Is there any way to print a particular bit of a digit after converting to binary form. Thanks in Advance, Praveena -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Extracting a bit of a scalar variable
On Apr 21, 2006, at 20:59, Praveena Vittal wrote: Is there any way to print a particular bit of a digit after converting to binary form. That can be easily done with substr y sprintf, this would take the second bit of 7 written in base 2: % perl -wle 'print substr sprintf(%b, 7), -2, 1' 1 That's quite readable, but if efficiency is an issue then pack/unpack might be better, it had to be measured in any case. -- fxn -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
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 : }' $ echo 3276767676 | perl -nle 'printf \n b %10d\n-- --\n, $_; for $b (0 .. 31) { printf %2d %10s\n, $b, $_ ($p = 1 $b) ? $p : }' For larger integers, look for bigint on CPAN. -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
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 ** b200 -- - 0 1 2 3 8 4 5 6 64 7128 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 ??? Derek Bellner Smith Unix Systems Engineer Cardinal Health Dublin, Ohio [EMAIL PROTECTED] 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
RE: Extracting a bit of a scalar variable
-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 ** b200 -- - 0 1 2 3 8 4 5 6 64 7128 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 ??? 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? 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
Re: Extracting a bit of a scalar variable
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 ** b200 -- - 0 1 2 3 8 4 5 6 64 7128 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: __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