From: "Lawrence D’Oliveiro" <ldo@nz.invalid>
Subject: Re: In precision typing we trust
Date: Sat, 30 Aug 2025 06:21:47 -0000 (UTC) [thread overview]
Message-ID: <108u59r$2eq9a$1@dont-email.me> (raw)
In-Reply-To: 108rs57$1t9nv$1@dont-email.me
On Fri, 29 Aug 2025 09:33:27 -0000 (UTC), Kevin Chadwick wrote:
> ... I don't believe you understand what I'm talking about as I
> believe Python does not have the ability for easily creating e.g.
> Unsigned_2, Unsigned_4 or Unsigned_15 and writing just those bits by
> assignment to wherever the programmer wants.
Something like the following? (Details of the BitVector class
omitted, but I can supply them if you’re interested):
+*In[2]:*+
[source, ipython3]
----
# constructor: v = BitVector(«width»[, «init»])
# «init» defaults to all zeroes if omitted
v1 = BitVector(3, 2)
v2 = BitVector(5)
v1, v2, int(v1), int(v2)
----
+*Out[2]:*+
----(BitVector(3, [False, True, False]),
BitVector(5, [False, False, False, False, False]),
2,
0)----
+*In[3]:*+
[source, ipython3]
----
# access can be single bits, or use standard Python slice notation
v1[0:2], v1[2], v1[1:3], v2[2:5]
----
+*Out[3]:*+
----(BitVector(2, [False, True]),
False,
BitVector(2, [True, False]),
BitVector(3, [False, False, False]))----
+*In[4]:*+
[source, ipython3]
----
v1[1:3] = [False, True]
v2[:] = 13
----
+*In[5]:*+
[source, ipython3]
----
v1, v2, int(v1), int(v2)
----
+*Out[5]:*+
----(BitVector(3, [False, False, True]),
BitVector(5, [True, False, True, True, False]),
4,
13)----
+*In[6]:*+
[source, ipython3]
----
# “+” does concatenation of BitVectors; note little-endian ordering.
v1 + v2, int(v1 + v2)
----
+*Out[6]:*+
----(BitVector(8, [False, False, True, True, False, True, True, False]), 108)----
+*In[7]:*+
[source, ipython3]
----
# shifts produce results with same number of bits
v2, v2 >> 3, v2 << 3, v2[:3] << 3, (v2 + BitVector(3)) << 3
----
+*Out[7]:*+
----(BitVector(5, [True, False, True, True, False]),
BitVector(5, [True, False, False, False, False]),
BitVector(5, [False, False, False, True, False]),
BitVector(3, [False, False, False]),
BitVector(8, [False, False, False, True, False, True, True, False]))----
+*In[8]:*+
[source, ipython3]
----
v1[1:3]
----
+*Out[8]:*+
----BitVector(2, [False, True])----
Following all raise errors
+*In[9]:*+
[source, ipython3]
----
v2[:1] = v1
----
+*Out[9]:*+
----
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[9], line 1
----> 1 v2[:1] = v1
[traceback details omitted]
ValueError: value has 3 bits, only allowed 1 for this usage
----
+*In[10]:*+
[source, ipython3]
----
v1[:] = 8
----
+*Out[10]:*+
----
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[10], line 1
----> 1 v1[:] = 8
[traceback details omitted]
ValueError: value has 4 bits, only allowed 3 for this usage
----
+*In[11]:*+
[source, ipython3]
----
v2[0] = 2
----
+*Out[11]:*+
----
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[11], line 1
----> 1 v2[0] = 2
[traceback details omitted]
ValueError: value has 2 bits, only allowed 1 for this usage
----
+*In[12]:*+
[source, ipython3]
----
v1[:] = v2[:]
----
+*Out[12]:*+
----
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[12], line 1
----> 1 v1[:] = v2[:]
[traceback details omitted]
ValueError: value has 5 bits, only allowed 3 for this usage
----
next prev parent reply other threads:[~2025-08-30 6:21 UTC|newest]
Thread overview: 125+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-18 10:29 In precision typing we trust Kevin Chadwick
2025-08-18 11:08 ` Dmitry A. Kazakov
2025-08-18 13:59 ` Niocláisín Cóilín de Ghlostéir
2025-08-18 15:03 ` Dmitry A. Kazakov
2025-08-18 22:52 ` Niocláisín Cóilín de Ghlostéir
2025-08-19 6:24 ` J-P. Rosen
2025-08-19 7:29 ` Dmitry A. Kazakov
2025-08-20 0:41 ` Lawrence D’Oliveiro
2025-08-20 15:49 ` Kevin Chadwick
2025-08-20 19:00 ` Dmitry A. Kazakov
2025-08-20 0:40 ` Lawrence D’Oliveiro
2025-08-20 13:59 ` Niocláisín Cóilín de Ghlostéir
2025-08-20 14:58 ` Dmitry A. Kazakov
2025-08-20 17:18 ` Niocláisín Cóilín de Ghlostéir
2025-08-20 19:04 ` Dmitry A. Kazakov
2025-08-20 20:38 ` Niocláisín Cóilín de Ghlostéir
2025-08-20 21:59 ` Lawrence D’Oliveiro
2025-08-20 23:37 ` Niocláisín Cóilín de Ghlostéir
2025-08-21 21:37 ` Lawrence D’Oliveiro
2025-08-21 1:31 ` Keith Thompson
2025-08-20 18:10 ` Niklas Holsti
2025-08-20 18:54 ` Kevin Chadwick
2025-08-22 1:27 ` Alastair Hogge
2025-08-22 12:49 ` Kevin Chadwick
2025-08-22 22:13 ` Lawrence D’Oliveiro
2025-08-22 23:21 ` Niocláisín Cóilín de Ghlostéir
2025-08-23 22:58 ` Lawrence D’Oliveiro
2025-08-24 8:37 ` Dmitry A. Kazakov
2025-08-24 11:05 ` Niocláisín Cóilín de Ghlostéir
2025-08-24 12:59 ` Dmitry A. Kazakov
2025-08-24 21:51 ` Lawrence D’Oliveiro
2025-08-24 21:50 ` Lawrence D’Oliveiro
2025-08-25 8:19 ` Dmitry A. Kazakov
2025-08-25 8:51 ` Paul Rubin
2025-08-25 10:41 ` Dmitry A. Kazakov
2025-08-25 17:12 ` Paul Rubin
2025-08-25 20:09 ` Dmitry A. Kazakov
2025-08-25 21:13 ` Lawrence D’Oliveiro
2025-08-26 7:59 ` Dmitry A. Kazakov
2025-08-27 0:13 ` Lawrence D’Oliveiro
2025-08-27 7:39 ` Dmitry A. Kazakov
2025-08-27 23:10 ` Lawrence D’Oliveiro
2025-08-27 23:49 ` Dmitry A. Kazakov
2025-08-28 0:35 ` Lawrence D’Oliveiro
2025-08-28 7:54 ` Dmitry A. Kazakov
2025-08-28 8:50 ` Kevin Chadwick
2025-08-28 9:02 ` Dmitry A. Kazakov
2025-08-25 21:27 ` Paul Rubin
2025-08-26 7:27 ` Lawrence D’Oliveiro
2025-08-25 21:10 ` Lawrence D’Oliveiro
2025-08-26 8:14 ` Dmitry A. Kazakov
2025-08-26 16:48 ` Paul Rubin
2025-08-26 17:01 ` Kevin Chadwick
2025-08-26 19:43 ` Dmitry A. Kazakov
2025-08-27 0:00 ` Paul Rubin
2025-08-27 0:14 ` Lawrence D’Oliveiro
2025-08-27 7:51 ` Dmitry A. Kazakov
2025-08-27 20:45 ` Keith Thompson
2025-08-28 8:38 ` Dmitry A. Kazakov
2025-08-28 9:00 ` Von Ottone
2025-08-27 23:16 ` Lawrence D’Oliveiro
2025-08-28 8:48 ` Dmitry A. Kazakov
2025-08-29 3:57 ` Lawrence D’Oliveiro
2025-08-29 7:53 ` Dmitry A. Kazakov
2025-08-30 22:29 ` Lawrence D’Oliveiro
2025-08-31 8:56 ` Dmitry A. Kazakov
2025-08-28 12:25 ` Björn Persson
2025-08-28 22:14 ` Lawrence D’Oliveiro
2025-08-29 15:30 ` Björn Persson
2025-08-30 3:34 ` Lawrence D’Oliveiro
2025-08-30 8:49 ` Kevin Chadwick
2025-08-30 22:34 ` Lawrence D’Oliveiro
2025-08-27 0:09 ` Lawrence D’Oliveiro
2025-08-27 9:06 ` Kevin Chadwick
2025-08-26 19:36 ` Dmitry A. Kazakov
2025-08-27 0:10 ` Lawrence D’Oliveiro
2025-08-27 7:57 ` Dmitry A. Kazakov
2025-08-27 23:12 ` Lawrence D’Oliveiro
2025-08-28 0:07 ` Dmitry A. Kazakov
2025-08-28 0:38 ` Lawrence D’Oliveiro
2025-08-28 8:00 ` Dmitry A. Kazakov
2025-08-26 9:06 ` Dmitry A. Kazakov
2025-08-23 0:26 ` Kevin Chadwick
2025-08-23 22:59 ` Lawrence D’Oliveiro
2025-08-23 23:58 ` Kevin Chadwick
2025-08-24 21:49 ` Lawrence D’Oliveiro
2025-08-25 8:19 ` Dmitry A. Kazakov
2025-08-25 20:58 ` Lawrence D’Oliveiro
2025-08-27 9:01 ` Kevin Chadwick
2025-08-27 9:24 ` Dmitry A. Kazakov
2025-08-27 23:07 ` Lawrence D’Oliveiro
2025-08-28 0:00 ` Dmitry A. Kazakov
2025-08-28 0:22 ` Lawrence D’Oliveiro
2025-08-28 0:11 ` Kevin Chadwick
2025-08-28 0:20 ` Kevin Chadwick
2025-08-28 0:33 ` Lawrence D’Oliveiro
2025-08-28 1:17 ` Alex // nytpu
2025-08-28 1:45 ` Lawrence D’Oliveiro
2025-08-28 8:24 ` Dmitry A. Kazakov
2025-08-29 3:53 ` Lawrence D’Oliveiro
2025-08-29 8:07 ` Dmitry A. Kazakov
2025-08-30 22:27 ` Lawrence D’Oliveiro
2025-08-31 9:15 ` Dmitry A. Kazakov
2025-08-28 9:17 ` Kevin Chadwick
2025-08-29 3:51 ` Lawrence D’Oliveiro
2025-08-29 9:27 ` Kevin Chadwick
2025-08-29 9:33 ` Kevin Chadwick
2025-08-30 6:21 ` Lawrence D’Oliveiro [this message]
2025-08-31 0:43 ` Kevin Chadwick
2025-08-31 7:30 ` Lawrence D’Oliveiro
2025-08-31 8:51 ` Kevin Chadwick
2025-08-28 9:04 ` Kevin Chadwick
2025-08-29 3:55 ` Lawrence D’Oliveiro
2025-08-29 9:41 ` Kevin Chadwick
2025-08-30 3:31 ` Lawrence D’Oliveiro
2025-08-28 9:05 ` Kevin Chadwick
2025-08-28 9:43 ` Dmitry A. Kazakov
2025-08-29 3:54 ` Lawrence D’Oliveiro
2025-08-29 8:15 ` Dmitry A. Kazakov
2025-08-30 22:25 ` Lawrence D’Oliveiro
2025-08-31 9:26 ` Dmitry A. Kazakov
2025-08-28 0:30 ` Lawrence D’Oliveiro
2025-08-20 19:10 ` Niocláisín Cóilín de Ghlostéir
2025-08-18 23:27 ` Lawrence D’Oliveiro
2025-08-18 23:46 ` Kevin Chadwick
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox