- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: how to use bit operator in ksh
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-12-2009 02:34 AM
тАО06-12-2009 02:34 AM
how to use bit operator in ksh
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-12-2009 07:20 AM
тАО06-12-2009 07:20 AM
Re: how to use bit operator in ksh
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-12-2009 09:09 AM
тАО06-12-2009 09:09 AM
Re: how to use bit operator in ksh
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-12-2009 10:10 AM
тАО06-12-2009 10:10 AM
Re: how to use bit operator in ksh
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-12-2009 02:27 PM
тАО06-12-2009 02:27 PM
Re: how to use bit operator in ksh
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-19-2009 06:13 PM
тАО06-19-2009 06:13 PM
Re: how to use bit operator in ksh
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"