#### Do you have a question? Post it now! No Registration Necessary. Now with pictures!

## Re: 25 ones

Try this:

#!/usr/bin/perl

print "Enter a number: ";

$x = <STDIN>; # Get input = number of 1s

$bits = 32; # Total number of bits

while ($bits > 0) {

if ($x > 0) { # If there are still 1s to add

$y .= 1; # Add a 1 to the string

} else { # Else no more 1s to add

$y .= 0; # Add a 0 to the string

}

$x--; $bits--;

$y .= " " if (($bits % 4) == 0); # Print a space every 4 bits

}

print "$y\n";

perl tmp.pl

Enter a number in hex: 25

1111 1111 1111 1111 1111 1111 1000 0000

John Black

## Re: 25 ones

On 7/9/2015 11:35 AM, T wrote:

Howdy.

#! /usr/bin/perl

use v5.014;

use strict;

use warnings;

my $binary = '0b' . '1' x 25 . '0' x 7; # binary representation

my $value = eval $binary; # numerical value

printf("Binary: %b\n", $value);

printf("Hexidecimal: %x\n", $value);

printf("Decimal: %d\n", $value);

Note that on line 5, the x operator is the "repeat" operator;

a x b returns a concatenation of b copies of a.

Example:

$blab = 'Blah' x 3; # $blab now contains 'BlahBlahBlah'

Also, the . operator is the "concatenation" operator.

Example:

$x = 'cat' . 'bird'; # $x now contains "catbird"

Thus the string $binary contains:

0b11111111111111111111111110000000

The "eval" evaluates the contents of $binary as if it was

Perl code, yielding it's numerical value.

The various printf statements then print the value in

several different number bases.

Well, it does change what Perl thinks the numeric value

of that integer is.

On a 64-bit system it is perceived as being +4294967168.

On a 32-bit system it is perceived as being -128.

(You'd probably get a "non-portable" warning, though.)

--

Cheers,

Robbie Hatley

Midway City, CA, USA

perl -le 'print "4o6e7o4f0w5llc7m"'

http://www.well.com/user/lonewolf/

https://www.facebook.com/robbie.hatley

## Re: 25 ones

Considering some of your other postings, you're probably dealing with a

netmask expressed as prefix length and want to turn that into the

corresponding number. The following function performs the corresponding

calculation ($_[0] is the first argument):

sub len

___2___mask

{

return ~((1 << (32 - $_[0])) - 1);

}

Explanation: Assuming $x is the prefix length,

32 - $x

is the length of the host part.

1 << (32 - $x)

is a number which has only the bit immediately above the left-most host

bit set (the lowest bit of the network address)

(1 << (32 - $x)) - 1

is thus a number where all 'host' bits are one. Consequently,

~((1 << (32 - $x)) - 1)

is the number with all network bits set to one.

## Re: 25 ones

On 10.07.15 14:27, Rainer Weikusat wrote:

For some range of values, this could also be written

sub len

{

return ~(int(2

}

But, since Perl values are handled in its own ways,

one might choose one or the other if abs $_[0] is at

or beyond some word size. (What size do words have

in modern networking?)

For some range of values, this could also be written

sub len

___2___mask_b{

return ~(int(2

******(32 - $_[0])) - 1);}

But, since Perl values are handled in its own ways,

one might choose one or the other if abs $_[0] is at

or beyond some word size. (What size do words have

in modern networking?)

## Re: 25 ones

I consider this 'needlessly cryptic'. Let's assume there are 5 host bits

in the address, it's immediately clear that

1 << 5

will be the binary number 100000, ie, a 1 followed by 5 zeroes, since

the 1 was shifted accross the 5 host bits. Numerically, that's 32/ 2

******5,

but it isn't really important/ helpful to know this in order to

understand the expression.

## Re: 25 ones

I'll assume that it isn't immediately clear that everyone is more

familiar with the definition of "<<" than with the definition of

"

******", or with bit fiddling, for that matter.

OTOH, if one approaches bit masks and knows a bit about hardware,

then having one or two symbols standing only for the values of a bit

would be splendid. The numeric interpretation is largely unnecessary,

then. Too many uses of the same symbols are necessarily confusing.

(But, hence, can support consultancy answering questions.)

## Re: 25 ones

Both are Perl operators. << exists in many other languages. I haven't

encountered

******anywhere else yet. More importantly, 'multiplication' is

a more general operation than what's needed for this special case and

understanding that the special case of moving a digit towards the 'left'

end of a number is the intended operation here requires knowledge of

'the magical constants' and their base 2 logarithms. Eg,

1 << 13

means 'shift 1 thirteen times to the left' (so that there are 13 0 bits

on the right side of it). Is that now 4096, 8192, 16386? Or was that

16383? After all, 'half of it minus one' was some special case, wasn't

it? And is 131072 the same as 1 << 17? What about 524288? And whats the

point of the 1544 here?

Granted, some of this doesn't apply when the 'order of magnitude magic

constants' are expressed as powers of the base, eg, 2

******(32 - 25), but

that's still roundabout for expressing operations which actually deal

with bits: I wouldn't want to write x << 2 + x when I mean x * 5

and I also don't want to write 2

******10 + 2

******9 + 2

******3 when I mean

1 << 10 | 1 << 9 | 1 << 3 (and absolutely not 1024 + 512 + 8 -- how's

that different from 1022 + 527 - 5?)

## Re: 25 ones

Thank you all! I have to reread everyone's responses

over very slowly, several times.

And yes, I am doing a netmask. The system tell me the

mask is 24, 25, or some such. This means the

number of 1's. I need to turn this into a

number that I can do a logical AND.

For instance, the "ip" command gives:

$ ip -f inet addr list br0

4: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state

UNKNOWN

inet 192.168.255.10/24 brd 192.168.255.255 scope global br0

I this instance, the mask is 24 or 0xffffff00. (24 ones)

25 would be 0xffffff80

From there, it is logical math to get the network.

For instance, 192.168.255.130/25, the "network" would be

192.168.255.128

Which would be

ip AND Mask

0xFFFFFF82 AND 0xFFFFFF80 = 0XFFFFFF80

Now to get the number 25 to turn into 0xFFFFFF80!

-T

Have no clue what will happen with a class B network!

Fortunately, I have none.

## Re: 25 ones

On 07/10/2015 01:26 PM, Rainer Weikusat wrote:

Would you explain what this means?

I presume shift always back fills with zeros. Am I correct?

Seems to me that if x=25, then we would be shift 0x01 (32-25 or 7)

bits to the left. This would be 0b0100 0000 or 0x40.

Then subtracting 1 from it for 0b0011 1111 or 0x3F. That is a

far piece away from 0xFFFFFF80

I don't get it.

Would you explain what this means?

I presume shift always back fills with zeros. Am I correct?

Seems to me that if x=25, then we would be shift 0x01 (32-25 or 7)

bits to the left. This would be 0b0100 0000 or 0x40.

Then subtracting 1 from it for 0b0011 1111 or 0x3F. That is a

far piece away from 0xFFFFFF80

I don't get it.

## Re: 25 ones

On 07/10/2015 06:24 PM, T wrote:

#!/usr/bin/perl -w

use strict;

my($a, $b);

$a = 25;

$b = ~( 1 << ( 32 - $a ) - 1 );

printf("The result of ~( 1 << ( 32 - $a ) - 1) is: $b\n");

$ perl shifttest2.pl

The result of ~( 1 << ( 32 - 25 ) - 1) is: 18446744073709551551

or 0xFFFFFFFFFFFFFFBF (Haven't figured out how to write in hex yet)

Should be 0xFFFFFFFFFFFFFF80

I am confused

*<code>*#!/usr/bin/perl -w

use strict;

my($a, $b);

$a = 25;

$b = ~( 1 << ( 32 - $a ) - 1 );

printf("The result of ~( 1 << ( 32 - $a ) - 1) is: $b\n");

*</code>*$ perl shifttest2.pl

The result of ~( 1 << ( 32 - 25 ) - 1) is: 18446744073709551551

or 0xFFFFFFFFFFFFFFBF (Haven't figured out how to write in hex yet)

Should be 0xFFFFFFFFFFFFFF80

I am confused

## Re: 25 ones

I did this elsewhere.

It's usually sensible to let the computer do the calculations because it

doesn't make errors like that:

[rw@doppelsaurus]~#perl -e 'printf("0x%08x (0b%032b)\n", $x = 1 << (32 - 25), $x)'

0x00000080 (0b00000000000000000000000010000000)

[rw@doppelsaurus]~#perl -e 'printf("0x%08x (0b%032b)\n", $x = (1 << (32 - 25)) - 1, $x)'

0x0000007f (0b00000000000000000000000001111111)

[rw@doppelsaurus]~#perl -e 'printf("0x%08x (0b%032b)\n", $x = ~((1 << (32 - 25)) - 1), $x)'

0xffffffffffffff80 (0b1111111111111111111111111111111111111111111111111111111110000000)

The innermost pair of brackets can be avoided, ie

~((1 << 32 - 25) - 1)

has the same result (but gcc will warn about that), the others are required.

## Re: 25 ones

On 07/10/2015 01:26 PM, Rainer Weikusat wrote:

Hi Ranier,

I took your expression apart so I could learn better what you did:

#!/usr/bin/perl

print "Enter your short mask: ";

$ShortMask = <STDIN>;

chomp ($ShortMask);

# ~((1 << (32 - $x)) - 1)

# Note: ~ is bitwise NOT (invert)

# << is Shift Left

$B = ( 32 - $ShortMask ); printf ( "B = 0x%0X %u\n", $B, $B );

$C = ( 1 << $B ); printf ( "C = 0x%0X %0b\n", $C, $C );

$D = ( $C - 1 ); printf ( "D = 0x%0X %0b\n", $D, $D );

$E = ~$D; printf ( "E = 0x%0X %0b\n", $E, $E );

$ perl MaskTest.pl

Enter your short mask: 26

B = 0x6 6

C = 0x40 1000000

D = 0x3F 111111

E = 0xFFFFFFFFFFFFFFC0

1111111111111111111111111111111111111111111111111111111111000000

How in the world did you figure that equation out ?! Dude! You

are the man!

Thank you again for helping me with this.

-T (a.k.a. Todd)

Hi Ranier,

I took your expression apart so I could learn better what you did:

*<code>*#!/usr/bin/perl

print "Enter your short mask: ";

$ShortMask = <STDIN>;

chomp ($ShortMask);

# ~((1 << (32 - $x)) - 1)

# Note: ~ is bitwise NOT (invert)

# << is Shift Left

$B = ( 32 - $ShortMask ); printf ( "B = 0x%0X %u\n", $B, $B );

$C = ( 1 << $B ); printf ( "C = 0x%0X %0b\n", $C, $C );

$D = ( $C - 1 ); printf ( "D = 0x%0X %0b\n", $D, $D );

$E = ~$D; printf ( "E = 0x%0X %0b\n", $E, $E );

*</code>*$ perl MaskTest.pl

Enter your short mask: 26

B = 0x6 6

C = 0x40 1000000

D = 0x3F 111111

E = 0xFFFFFFFFFFFFFFC0

1111111111111111111111111111111111111111111111111111111111000000

How in the world did you figure that equation out ?! Dude! You

are the man!

Thank you again for helping me with this.

-T (a.k.a. Todd)

#### Site Timeline

- » how do I print hex inside stings?
- — Next thread in » PERL Discussions

- » Encoding: All or None?
- — Previous thread in » PERL Discussions

- » s suffix question
- — Newest thread in » PERL Discussions

- » SSD partition alignment revisited
- — The site's Newest Thread. Posted in » Computer Hardware