• Home
  • About

szehau's weblog

Life, Internet, Software, Gadgets, Programming and Investments

  • life log
  • internet
  • programming
  • software
  • working holiday
  • gadget
  • project

PHP integer and float

August 7, 2005 23:27 by Sze Hau 1 Comment

I found a problem while trying to do some bitwise operations (e.g. 32 bits XOR) in PHP. Consider the following case:


$a = -3036738507; // stored in float type
$b = 98758; // stored in integer type
$c= ($a & 0xFFFFFFFF) ^ $b;

Note: In PHP, if you specify a number beyond the bounds of the integer type, it will be interpreted as a float instead.

When I run the code in Windows, it gives me the correct value: $c = 1258196467. But when I run the code in Linux, it gives me the incorrect value: $c = -2147384890 (it seems like an integer overflow).

4AFF0C35 ^ 181C6 = 4AFE8DF3

Is this a bug in PHP? I tried shift bits and type casting, it doesn’t give me the corrent answers in Linux.


$t1 = ($a>>24) & 0xFF; $t2 = ($a>>16) & 0xFF; $t1 = ($a>>8) & 0xFF; $t1 = ($a>>0) & 0xFF;
$a = ($t1< <24) | ($t2<<16) | ($t3<<8) | ($t4<<0); $c= $a ^ $b;

and


$c= ((int)$a) ^ $b;

Anyway, I have a quick solution for it:


function cvt32($a) {
$maxInt = (float)(0xFFFFFFFF +1);
$temp = round ($a / $maxInt);
$out = $a - $temp * $maxInt;
return (int) $out;
}
$a = -3036738507; // stored in float type
$b = 98758; // stored in integer type
$c= cvt32($a) ^ $b;

The function cvt32() above takes 32 bits (least significant bits) from the float and return it as a integer so that PHP can do 32 bits bitwise operation correctly.

More from my site

  • Jung Won – Korea BBQ Restaurant Near Jalan AmpangJung Won – Korea BBQ Restaurant Near Jalan Ampang
  • Adsense Notifier 0.7 is now availableAdsense Notifier 0.7 is now available
  • E-Genting Programming Competition 2006E-Genting Programming Competition 2006
  • Pureen Warehouse Sales & Stock ClearancePureen Warehouse Sales & Stock Clearance
  • Subscribed mobile broadbandSubscribed mobile broadband
  • Holiday at Motureka, KaiteriteriHoliday at Motureka, Kaiteriteri

Filed Under: programming

About Sze Hau

Geek. Love programming. Coffee addicted. Married with a child. Working towards financial freedom.

Comments

  1. EngLee says

    August 9, 2005 08:47 at 8:47 am

    You seem like enjoy programming too much!

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

  • Facebook
  • Google+
  • Instagram
  • Twitter

More Posts

5 Essential Things You Need To Have At Your Home During The COVID-19 Pandemic

June 1, 2021 22:15 By Sze Hau Leave a Comment

LED Downlight

Troubleshooting A Flickering LED Downlight

February 13, 2016 00:29 By Sze Hau Leave a Comment

Repair A Broken iPhone Lightning Cable

January 30, 2016 00:54 By Sze Hau Leave a Comment

Sponsored Links

Blog Network

  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • Snippet IT Information technology news, ideas and experiences and opinions

szehau’s weblog

szehau is a software programmer that has strong interest in the world of programming and Internet. He is graduated in University of Malaya, Malaysia with a Bachelor’s degree in Software Engineering. He draws on his passsion for web programming to share with others how to build a successful websites.

Recent

  • 5 Essential Things You Need To Have At Your Home During The COVID-19 Pandemic
  • Troubleshooting A Flickering LED Downlight
  • Repair A Broken iPhone Lightning Cable
  • New Gadget – MacBook Pro with Retina Display (2015)
  • One Watt LED Night Light

Search

Tags

adsense advertisement baby blog blogtal Canon EOS 400D Chinese New Year Christchurch CNY DiGi electronic Facebook FBM Flickr Flickr Uploadr Google Google Maps Google Search Results Google Webmaster Central H1N1 income tax investment joke KLCI KLSE Lake Tekapo Maxis Maybank2u Motueka movie mutual funds New Zealand PC Fair 2009 Queenstown restaurant for dinner sharing stock trackback travel Twitter unit trust visa wordpress wordpress plugin working holiday

Copyright © 2023 · Magazine Pro Theme on Genesis Framework · WordPress · Log in