Integer/Number and uint(8,16,32,64bit) fun... ;)
This commit is contained in:
parent
c641599366
commit
6d3456b62c
6 changed files with 182 additions and 6 deletions
42
examples/uint_types.fun
Executable file
42
examples/uint_types.fun
Executable file
|
|
@ -0,0 +1,42 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the ISC license.
|
||||
* https://opensource.org/license/isc-license-txt
|
||||
*/
|
||||
|
||||
// Unsigned integer type examples with wrapping behavior
|
||||
|
||||
uint8 u8 = 255
|
||||
uint16 u16 = 65535
|
||||
uint32 u32 = 4294967295
|
||||
uint64 u64 = 18446744073709551615 // will wrap to 64-bit
|
||||
|
||||
print("u8 = " + to_string(u8) + " :: " + typeof(u8))
|
||||
print("u16 = " + to_string(u16) + " :: " + typeof(u16))
|
||||
print("u32 = " + to_string(u32) + " :: " + typeof(u32))
|
||||
print("u64 = " + to_string(u64) + " :: " + typeof(u64))
|
||||
|
||||
// Demonstrate wrapping on assignment
|
||||
u8 = 256 // wraps to 0
|
||||
u16 = 65536 // wraps to 0
|
||||
u32 = 4294967296 // wraps to 0
|
||||
u64 = 18446744073709551616 // wraps to 0
|
||||
|
||||
print("wrap u8 -> " + to_string(u8))
|
||||
print("wrap u16 -> " + to_string(u16))
|
||||
print("wrap u32 -> " + to_string(u32))
|
||||
print("wrap u64 -> " + to_string(u64))
|
||||
|
||||
// Mixed arithmetic then wrapping
|
||||
u8 = 250
|
||||
u8 = u8 + 10 // 260 -> wrap 260 % 256 = 4
|
||||
print("u8 after +10 wrap -> " + to_string(u8))
|
||||
|
||||
// 'number' maps to 64-bit unsigned behavior
|
||||
number n = -1 // will clamp/wrap to 64-bit (implementation-defined display)
|
||||
print("number n = " + to_string(n) + " :: " + typeof(n))
|
||||
Loading…
Add table
Add a link
Reference in a new issue