Operating System - HP-UX
1753867 Members
7341 Online
108809 Solutions
New Discussion юеВ

Re: how to use bit operator in ksh

 
mobidyc
Trusted Contributor

how to use bit operator in ksh

Hello,

i want to calcul the broadcast using a subnet address and its netmask.
for now, it works with an ip conversion to binary ad a mix of printf and bc but i find my way ugly.

how can i use the bits operators (& or |) to calcul these values ?

here is the way i use for the moment:
SUB=10.146.46.0
MASK=255.255.255.0

SUB_1="$(/usr/bin/printf "%.8i\n" $(echo "obase=2 ; $(echo $SUB |cut -d. -f1)" |bc))"
MASK_1="$(/usr/bin/printf "%.8i\n" $(echo "obase=2 ; `echo $MASK |cut -d. -f1`" |bc) |sed -e 's/0/2/g' -e 's/1/0/g' -e 's/2/1/g')"
BROADCAST_1="$(echo "ibase=2 ; "$(echo "$SUB_1 + $MASK_1" |bc |sed 's/[2-9]/1/g') |bc)"

as you can see, i think this code could be largely improved

All advices appreciated.

Cheers,
Cedrick Gaillard
Best regards, Cedrick Gaillard
5 REPLIES 5
mobidyc
Trusted Contributor

Re: how to use bit operator in ksh

The solution is the following:
typeset -i2 LIMITMASK=255

SUB=10.146.46.0
MASK=255.255.255.0

typeset -i2 SUB_1=$(echo $SUB |cut -d. -f1)
typeset -i2 MASK_1=$(echo $MASK |cut -d. -f1)
typeset -i10 BROADCAST_1=$(( $SUB_1 | ($LIMITMASK ^ $MASK_1) ))

This method divide by two the traitment.
Best regards, Cedrick Gaillard
Dennis Handly
Acclaimed Contributor

Re: how to use bit operator in ksh

>how can I use the bit operators (& or |) to calculate these values?

You can use bit operators in arithmetic expressions: (( ))

If you need to use other bases, you can use:
typeset -i16 x=32; echo $x # It should display 16#20.

(( y = 7 & 4 ))
This will set y to 4.
mobidyc
Trusted Contributor

Re: how to use bit operator in ksh

Thanks Dennis.
but the purpose of my question was:
what is the method to calculate broadcast using the bitwise operators.

my sentence was not enough explicit, I admit it. ;-)

anyway, I've solved the problem myself.

Thanks.
Cedrick Gaillard
Best regards, Cedrick Gaillard
mobidyc
Trusted Contributor

Re: how to use bit operator in ksh

For information.
here is the final script :

#!/usr/bin/ksh

# Informations a traiter
IP=192.168.2.53
MASK=255.255.224.0

# limite des bits IP
MASK_LIMIT=255

# s├йparation des champs IP et NETMASK
IP_1=$(echo $IP |cut -d. -f1)
IP_2=$(echo $IP |cut -d. -f2)
IP_3=$(echo $IP |cut -d. -f3)
IP_4=$(echo $IP |cut -d. -f4)

MASK_1=$(echo $MASK |cut -d. -f1)
MASK_2=$(echo $MASK |cut -d. -f2)
MASK_3=$(echo $MASK |cut -d. -f3)
MASK_4=$(echo $MASK |cut -d. -f4)

SUBNET="$(($IP_1 & $MASK_1)).$(($IP_2 & $MASK_2)).$(($IP_3 & $MASK_3)).$(($IP_4 & $MASK_4))"
BROADCAST="$(($IP_1 | ($MASK_LIMIT ^ $MASK_1))).$(($IP_2 | ($MASK_LIMIT ^ $MASK_2))).$(($IP_3 | ($MASK_LIMIT ^ $MASK_3))).$(($IP_4 | ($MASK_LIMIT ^ $MASK_4)))"

echo " IP : $IP"
echo " NETMASK : $MASK"
echo " SUBNET : $SUBNET"
echo "BROADCAST : $BROADCAST"

exit 0
Best regards, Cedrick Gaillard
Dennis Handly
Acclaimed Contributor

Re: how to use bit operator in ksh

Here is your initial base two output, zeroed filled to 8 chars:
SUB=10.146.46.0
MASK=255.255.255.0

typeset -i2 SUB_1x=$(echo $SUB | awk -F. '{print $1}')
typeset -Z8 SUB_1z=${SUB_1x#2#}
echo "SUB: $SUB_1z"

typeset -i2 MASK_1x=$(echo $MASK | awk -F. '{print $1}')
(( MASK_1x = ~MASK_1x & 16#ff ))
typeset -Z8 MASK_1z=${MASK_1x#2#}
echo "MASK: $MASK_1z"

typeset -i2 BROADCAST_1x
(( BROADCAST_1x = SUB_1x | MASK_1x ))
typeset -Z8 BROADCAST_1z=${BROADCAST_1x#2#}
echo "BROADCAST: $BROADCAST_1z"