@@ -2423,6 +2423,179 @@ public static function erfVal($x)
2423
2423
return self ::$ twoSqrtPi * $ sum ;
2424
2424
}
2425
2425
2426
+ /**
2427
+ * Validate arguments passed to the bitwise functions.
2428
+ *
2429
+ * @param mixed $value
2430
+ *
2431
+ * @throws Exception
2432
+ *
2433
+ * @return int
2434
+ */
2435
+ private static function validateBitwiseArgument ($ value )
2436
+ {
2437
+ $ value = Functions::flattenSingleValue ($ value );
2438
+
2439
+ if (is_int ($ value )) {
2440
+ return $ value ;
2441
+ } elseif (is_numeric ($ value )) {
2442
+ if ($ value == (int ) ($ value )) {
2443
+ $ value = (int ) ($ value );
2444
+ if (($ value > pow (2 , 48 ) - 1 ) || ($ value < 0 )) {
2445
+ throw new Exception (Functions::NAN ());
2446
+ }
2447
+
2448
+ return $ value ;
2449
+ }
2450
+
2451
+ throw new Exception (Functions::NAN ());
2452
+ }
2453
+
2454
+ throw new Exception (Functions::VALUE ());
2455
+ }
2456
+
2457
+ /**
2458
+ * BITAND.
2459
+ *
2460
+ * Returns the bitwise AND of two integer values.
2461
+ *
2462
+ * Excel Function:
2463
+ * BITAND(number1, number2)
2464
+ *
2465
+ * @category Engineering Functions
2466
+ *
2467
+ * @param int $number1
2468
+ * @param int $number2
2469
+ *
2470
+ * @return int|string
2471
+ */
2472
+ public static function BITAND ($ number1 , $ number2 )
2473
+ {
2474
+ try {
2475
+ $ number1 = self ::validateBitwiseArgument ($ number1 );
2476
+ $ number2 = self ::validateBitwiseArgument ($ number2 );
2477
+ } catch (Exception $ e ) {
2478
+ return $ e ->getMessage ();
2479
+ }
2480
+
2481
+ return $ number1 & $ number2 ;
2482
+ }
2483
+
2484
+ /**
2485
+ * BITOR.
2486
+ *
2487
+ * Returns the bitwise OR of two integer values.
2488
+ *
2489
+ * Excel Function:
2490
+ * BITOR(number1, number2)
2491
+ *
2492
+ * @category Engineering Functions
2493
+ *
2494
+ * @param int $number1
2495
+ * @param int $number2
2496
+ *
2497
+ * @return int|string
2498
+ */
2499
+ public static function BITOR ($ number1 , $ number2 )
2500
+ {
2501
+ try {
2502
+ $ number1 = self ::validateBitwiseArgument ($ number1 );
2503
+ $ number2 = self ::validateBitwiseArgument ($ number2 );
2504
+ } catch (Exception $ e ) {
2505
+ return $ e ->getMessage ();
2506
+ }
2507
+
2508
+ return $ number1 | $ number2 ;
2509
+ }
2510
+
2511
+ /**
2512
+ * BITXOR.
2513
+ *
2514
+ * Returns the bitwise XOR of two integer values.
2515
+ *
2516
+ * Excel Function:
2517
+ * BITXOR(number1, number2)
2518
+ *
2519
+ * @category Engineering Functions
2520
+ *
2521
+ * @param int $number1
2522
+ * @param int $number2
2523
+ *
2524
+ * @return int|string
2525
+ */
2526
+ public static function BITXOR ($ number1 , $ number2 )
2527
+ {
2528
+ try {
2529
+ $ number1 = self ::validateBitwiseArgument ($ number1 );
2530
+ $ number2 = self ::validateBitwiseArgument ($ number2 );
2531
+ } catch (Exception $ e ) {
2532
+ return $ e ->getMessage ();
2533
+ }
2534
+
2535
+ return $ number1 ^ $ number2 ;
2536
+ }
2537
+
2538
+ /**
2539
+ * BITLSHIFT.
2540
+ *
2541
+ * Returns the number value shifted left by shift_amount bits.
2542
+ *
2543
+ * Excel Function:
2544
+ * BITLSHIFT(number, shift_amount)
2545
+ *
2546
+ * @category Engineering Functions
2547
+ *
2548
+ * @param int $number
2549
+ * @param int $shiftAmount
2550
+ *
2551
+ * @return int|string
2552
+ */
2553
+ public static function BITLSHIFT ($ number , $ shiftAmount )
2554
+ {
2555
+ try {
2556
+ $ number = self ::validateBitwiseArgument ($ number );
2557
+ } catch (Exception $ e ) {
2558
+ return $ e ->getMessage ();
2559
+ }
2560
+
2561
+ $ shiftAmount = Functions::flattenSingleValue ($ shiftAmount );
2562
+
2563
+ $ result = $ number << $ shiftAmount ;
2564
+ if ($ result > pow (2 , 48 ) - 1 ) {
2565
+ return Functions::NAN ();
2566
+ }
2567
+
2568
+ return $ result ;
2569
+ }
2570
+
2571
+ /**
2572
+ * BITRSHIFT.
2573
+ *
2574
+ * Returns the number value shifted right by shift_amount bits.
2575
+ *
2576
+ * Excel Function:
2577
+ * BITRSHIFT(number, shift_amount)
2578
+ *
2579
+ * @category Engineering Functions
2580
+ *
2581
+ * @param int $number
2582
+ * @param int $shiftAmount
2583
+ *
2584
+ * @return int|string
2585
+ */
2586
+ public static function BITRSHIFT ($ number , $ shiftAmount )
2587
+ {
2588
+ try {
2589
+ $ number = self ::validateBitwiseArgument ($ number );
2590
+ } catch (Exception $ e ) {
2591
+ return $ e ->getMessage ();
2592
+ }
2593
+
2594
+ $ shiftAmount = Functions::flattenSingleValue ($ shiftAmount );
2595
+
2596
+ return $ number >> $ shiftAmount ;
2597
+ }
2598
+
2426
2599
/**
2427
2600
* ERF.
2428
2601
*
0 commit comments