Bitwise Semantics · Delphi → C# / C++ / Java
Why Delphi shr silently breaks when ported to C# >>.
Unit tests that only pass positive integers stay 100% green after porting X shr N to C# x >> n. The moment a production payload sets the sign bit (hash, CRC, packed flags, serialization), the output diverges silently.
Exact semantic difference
- Delphi
shr: Always logical (unsigned, zero-filling) — even when the left operand is signedInteger(32-bit) orInt64(64-bit). Example:Integer(-1) shr 1 = $7FFFFFFF(2147483647). - C#
>>onint/long: Arithmetic (sign-extending) —(-1) >> 1 == -1! To preserve Delphi semantics in C#, you must use C# 11+ unsigned right shiftx >>> nor an explicit unsigned cast(int)((uint)x >> n). - Java:
>>is arithmetic; Delphishrmaps to Java>>>. - C++:
>>on signed integers is arithmetic in C++20 (and implementation-defined in older standards); cast explicitly touint32_t/uint64_tbefore shifting.
Before / After port comparison
// Delphi (signed Integer, logical shift): var V: Integer; begin V := -1; Writeln(V shr 4); // 268435455 ($0FFFFFFF) end; // BROKEN C# port (compiles with 0 warnings!): int v = -1; Console.WriteLine(v >> 4); // -1 (WRONG!) // CORRECT C# 11+ port: Console.WriteLine(v >>> 4); // 268435455 ($0FFFFFFF)
In Delphi Behavior Contracts, intermediate arithmetic width, signedness, and bit-shift rules are enforced systematically as Contract Dimension #6 during every port and review.
Related guides: Dynamic array aliasing · System.Round & uses shadowing.
Verify it yourself in Delphi 13 (Free < 1 MB Demo)
Every rule in these guides comes from 879 build observations and 650 executable runs on Delphi 13 Community Edition (Win32, Win64, and compile-only Android ARM). The free demo on GitHub includes the complete 74-chapter & 89-directive Table of Contents, 6 multi-difficulty excerpts, 4 complete directive cards, 8 compiler JSON receipts, 6 runnable .dpr probes, and working plugins, skills, and local offline MCP server.
Get Full Universal Suite · from $99Inspect Free Demo on GitHubAll Technical Guides
Honest scope boundaries: Strictly a Delphi / Object Pascal language, runtime-behavior, and compiler-directive reference — not a VCL, FMX, or FireDAC component API encyclopedia. Does not claim 100% coverage of the language or any ABI; open questions are listed explicitly by name. Independent product, not affiliated with Embarcadero Technologies, Inc.