473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Problems using bitwise &


I'm trying to perform some basic mathematics on IP Addresses. IP's are
normally represented as 32 bit unsigned integers. However I am getting
strange results when using the operators in PHP.

If you run this is a mysql query browser: select (3388826209 &
4294867288) as val; it will tell you the correct answer is:
3388760128. This is the answer I want. My snippet of PHP code is here:

$ip = $networkObjectRow['sipv4addr'];
$subnet = $networkObjectRow['eipv4addr'];

//make php treats these likes numbers and not strings

$ip += (int)0;
$subnet += (int)0;

if ($ip == 0)
return "NOT SET";

$ans = $ip & $subnet; //Bitwise AND.

print("<!-- sip F (ip=$ip,subnet=$subnet) returned is
$ans -->\n");
return ($ans);

However my output is like this:

<!-- sip F (ip=170419457,subnet=4294967040) returned is 170419456 -->
<!-- sip F (ip=170419457,subnet=4294967040) returned is 170419456 -->
<!-- sip F (ip=3388826225,subnet=4294967280) returned is -906141072 --
>
<!-- sip F (ip=3388826225,subnet=4294967280) returned is -906141072 --
>
<!-- sip F (ip=170419969,subnet=4294967040) returned is 170419968 -->

<!-- sip F (ip=170419969,subnet=4294967040) returned is 170419968 -->

Where the IP starts 17... the answer is correct. When the value starts
3388 the value is wrong.

I guess this is because PHP is treating the number as a 64 bit one
rather than an unsigned 32 bit one but I'm not really sure. Can anyone
help please?
Mar 28 '08 #1
1 1239
Philluminati wrote:
>
$ip = $networkObjectRow['sipv4addr'];
$subnet = $networkObjectRow['eipv4addr'];

//make php treats these likes numbers and not strings

$ip += (int)0;
$subnet += (int)0;
$ip = (int)$networkObjectRow['sipv4addr'];
$subnet = (int)$networkObjectRow['eipv4addr'];

>
print("<!-- sip F (ip=$ip,subnet=$subnet) returned is
$ans -->\n");
return ($ans);

However my output is like this:

<!-- sip F (ip=170419457,subnet=4294967040) returned is 170419456 -->
<!-- sip F (ip=170419457,subnet=4294967040) returned is 170419456 -->
<!-- sip F (ip=3388826225,subnet=4294967280) returned is -906141072 --
<!-- sip F (ip=3388826225,subnet=4294967280) returned is -906141072 --
<!-- sip F (ip=170419969,subnet=4294967040) returned is 170419968 -->

<!-- sip F (ip=170419969,subnet=4294967040) returned is 170419968 -->

Where the IP starts 17... the answer is correct. When the value starts
3388 the value is wrong.
PHP _outputs_ it as signed integer. Try formatted output to clear
this question:

printf("<!-- sip F (ip=%08X,subnet=%08X) returned is %08X
-->\n",$ip,$subnet,$ans);
Mar 28 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michael Fork | last post by:
Note: I pasted the code the attachments as plain text after the message (I wasn't able to post it with an attachment...) Attached are the XSL and XML files that I am having problems with. I am...
9
by: Francesco Moi | last post by:
Hello. I'm trying to build a RSS feed for my website. It starts: ----------------//--------------------- <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE rss PUBLIC "-//Netscape...
1
by: Eric | last post by:
I am trying to figure out a good way to implement a XSLT transformation. Basically my goal is that I want to be able to ouput the following XML in a document: <chart type="pie" width="100"...
14
by: Tony Johansson | last post by:
Hello Experts! Assume I have a class called SphereClass as the base class and a class called BallClass that is derived from the SphereClass. The copy constructor initialize the left hand object...
12
by: sandy_pt_in | last post by:
How to mulitply two integer numbers using bitwise operators in C language.Please reply as early as possible
6
by: jawilson2 | last post by:
Hello, I have some data that stores two 16-bit integers in a 32-bit float, and I would like to extract the data. I want to do something like ...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
3
by: Jay Ruyle | last post by:
I'm trying to figure out a way to list several items in a listbox and let the user select any number of items in the listbox. I have tried to code in the items as bitwise items but all it stores...
0
by: Hamayun Khan | last post by:
Hi I m developing site for my client using asp.net 2.0. He asked me for URL rewriting. which I have done using Intelligencia.UrlRewriter.RewriterHttpModule rewriting module. I write the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.