473,569 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Shift Confusion

I'm trying to pack two characters into a single byte, and the shifting
in Python has me confused.

Essentially, it should be possible to use a 'packed string' format in
Python, where as long as the characters you're sending are in the ASCII
range 0 to 127, two will fit in a byte.

Here's the code. Can you tell what I'm doing wrong?

|import types
|
|def PackString(s):
| if type(s) != types.StringTyp e:
| raise Exception("This routine only packs strings!")
| l = len(s)
| if l % 2 != 0:
| s = s + '\0'
| l += 1
| chars = []
| for i in range(0, l, 2):
| x = ord(s[i])
| y = ord(s[i+1])
| chars.append(ch r((y << 1) | x))
| return ''.join(chars)
|
|def UnpackString(s) :
| if type(s) != types.StringTyp e:
| raise Exception("This routine only unpacks strings!")
| l = len(s)
| chars = []
| for i in range(l):
| temp = ord(s[i])
| c = 0xf0 & temp
| chars.append(ch r(c))
| c = 0x0f & temp
| chars.append(ch r(c))
| return ''.join(chars)
|
|
|def main():
| s = "Test string"
| print s
| packed = PackString(s)
| print "This is the string packed:"
| print packed
| print "This is the string unpacked:"
| print UnpackString(pa cked)
|
|main()

Jul 18 '05 #1
10 1929
On 23 Feb 2005 22:06:54 -0800, "Kamilche" <kl*******@comc ast.net>
wrote:
I'm trying to pack two characters into a single byte, and the shifting
in Python has me confused.

Essentially, it should be possible to use a 'packed string' format in
Python, where as long as the characters you're sending are in the ASCII
range 0 to 127, two will fit in a byte.


It should be possible, but only in a realm where phlogiston and
perpetual motion machines exist.

To hold one ASCII character 0 <= ord(c) < 128, you need log2(128) == 7
bits. There are 8 bits in a byte. Therefore you can hold only 8/7.0 ==
1.14... ASCII characters in a standard 8-bit byte. If there is such a
thing as a 14-bit byte, that's news to me.

Other things you are doing wrong:

1. Using "L".lower() as a variable name. Some fonts make it extremely
hard to work out what is what in "l1l1l1l1l1l1ll 1l1l" -- can you read
that???

2. Hmmm:
"chr((y << 1) | x))".upper() 'CHR((Y << 1) | X))'

OK so that's a one, not the length of your string, as augmented.
Either would be be wrong. Shifting a character left ONE bit (or,
changing the emphasis, one BIT) and then ORing in another character
would be vaguely reasonable only if you were writing a hash function.

3. Supposing this modern alchemy had worked: when unpacking, how would
you know whether the string was originally (say) seven characters long
or 8 characters long?

4. Your unpacking routine appears to be trying to unpack two 4-bit
items (nibbles) out of a byte, but is not doing (temp & 0xf0) >> 4 for
the top nibble as one might expect ..... aaahhh!!?? are you trying to
emulate packed decimal???

5. Not writing down a clear statement of what you are trying to do,
followed by examples of input and expected output. This latter goes by
fancy names like "test-driven development"; when I started programming
it was known as "common sense".

6. Not using Python interactively to explore what's going on:
ord('x') 120 ord('y') 121 (120 << 1) 240 (120 << 1) | 121 249


HTH,
John
Jul 18 '05 #2
At programming level it seems correct (a part a "return" closure
needed for the "main" function).

But the error is IMHO conceptual:
for a char you need 7 bits (from 0 to 127 or in hex from x00 to x7F)
and you can't accomodate the other char in only one bit!
The other 128 symbols (from 128 to 255 or in hex from x80 to xFF) are
only possible because you use again 7 bits, but with the 8th bit set
to 1!

What you are trying to do I made in C language (some years ago...)
using however bytes and words, packing 2 bytes in only one word, but
you can't pack 2 chars (each one beeing nearly a byte) in a byte!
Jul 18 '05 #3

"John Machin" <sj******@lexic on.net> wrote in message
news:u8******** *************** *********@4ax.c om...
Essentially, it should be possible to use a 'packed string' format in
Python, where as long as the characters you're sending are in the ASCII
range 0 to 127, two will fit in a byte.


It should be possible, but only in a realm where phlogiston and
perpetual motion machines exist.


alt.sys.pdp10 ?
Jul 18 '05 #4
On 23 Feb 2005 22:06:54 -0800, "Kamilche" <kl*******@comc ast.net>
declaimed the following in comp.lang.pytho n:

Essentially, it should be possible to use a 'packed string' format in
Python, where as long as the characters you're sending are in the ASCII
range 0 to 127, two will fit in a byte.
Pardon? You are going to fit TWO 7-bit values into one 8-bit?
-- =============== =============== =============== =============== == <
wl*****@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Jul 18 '05 #5
On Thu, 24 Feb 2005 14:22:59 -0000, "Richard Brodie" <R.******@rl.ac .uk>
declaimed the following in comp.lang.pytho n:

"John Machin" <sj******@lexic on.net> wrote in message
news:u8******** *************** *********@4ax.c om...
Essentially, it should be possible to use a 'packed string' format in
Python, where as long as the characters you're sending are in the ASCII
range 0 to 127, two will fit in a byte.
It should be possible, but only in a realm where phlogiston and
perpetual motion machines exist.


alt.sys.pdp10 ?

Closest thing I know of to what is being attempted is DEC's
RAD-50; but that was essentially just uppercase A..Z, 0..9, and a few
punctuation marks, and packed three of them into two bytes.

-- =============== =============== =============== =============== == <
wl*****@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Jul 18 '05 #6
"Dennis Lee Bieber" <wl*****@ix.net com.com> wrote in message
news:pj******** *************** *********@4ax.c om...
On 23 Feb 2005 22:06:54 -0800, "Kamilche" <kl*******@comc ast.net>
declaimed the following in comp.lang.pytho n:

Essentially, it should be possible to use a 'packed string' format in
Python, where as long as the characters you're sending are in the ASCII
range 0 to 127, two will fit in a byte.

Pardon? You are going to fit TWO 7-bit values into one 8-bit?


Quite. Although you can sort of see how one might naively arrive at this
conclusion: one 7-bit char takes 0...127, which when you put it into an
8-bit byte leaves 128...255 unused for a second char....

James
Jul 18 '05 #7
Dennis Lee Bieber wrote:
On Thu, 24 Feb 2005 14:22:59 -0000, "Richard Brodie" <R.******@rl.ac .uk>
declaimed the following in comp.lang.pytho n:

"John Machin" <sj******@lexic on.net> wrote in message
news:u8****** *************** ***********@4ax .com...

Essentially , it should be possible to use a 'packed string' format in
Python, where as long as the characters you're sending are in the ASCII
range 0 to 127, two will fit in a byte.

It should be possible, but only in a realm where phlogiston and
perpetual motion machines exist.


alt.sys.pdp 10 ?


Closest thing I know of to what is being attempted is DEC's
RAD-50; but that was essentially just uppercase A..Z, 0..9, and a few
punctuation marks, and packed three of them into two bytes.

Another code it used was known as SIXBIT, allowing 64 different
characters. IIRC it could cope with letters, digits and a bunch of
punctuation - see

http://nemesis.lonestar.org/referenc...es/sixbit.html

The DECSystem-10 used a 3-6 bit word, so you could get six sixbit
characters to a word. In ASCII you could only get four (or, if you threw
the parity bit away, five) characters to a word.

While its character-handling instructions weren't, as I recall, unique
in the industry, the DECSystem-10 remains the only hardware I ever got
to use that had instructions to handle variable byte sizes.

regards
Steve

Jul 18 '05 #8
> Quite. Although you can sort of see how one might naively arrive at
this
conclusion: one 7-bit char takes 0...127, which when you put it into an 8-bit byte leaves 128...255 unused for a second char....

James


Yep, that's what I was doing. Guess I was too tired to program usefully
last night.

Thanks for clearing that up, guys!

Jul 18 '05 #9
On Thu, 24 Feb 2005 09:41:56 -0800, "James Kew" <ja*******@gmai l.com>
declaimed the following in comp.lang.pytho n:
Quite. Although you can sort of see how one might naively arrive at this
conclusion: one 7-bit char takes 0...127, which when you put it into an
8-bit byte leaves 128...255 unused for a second char....
Heh... Which can be proven fairly false by just looking at the
old Wendy's advertising (I mean OLD -- like 1979-81). In counter to the
BK "Have it your way", Wendy's used to advertising 256 different ways to
make a burger -- the big feature was that /they/ didn't make the 256
versions. They had a condiment table with 8 toppings, and the buyer had
to put the toppings on... 8 toppings, on/off each, 256 different
combinations...

-- =============== =============== =============== =============== == <
wl*****@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2456
by: Chris Williams | last post by:
Hi all, I am becoming a little stressed with the age-old problem of detecting when a user types (for example) a quote character ("). On a UK keyboard, I can look for a 2 + shift mask; on US, single quote + shift mask. I don't want to have to know every single international keyboard layout just to know when a shifted character has been...
43
26459
by: Mehta Shailendrakumar | last post by:
Hello, Can anyone suggest me operator to perform arithmetic shift in C? May it be for a perticular compiler. Thank you in advance. Regards, Shailendra
7
2903
by: Marty McFly | last post by:
Hello VB Gurus, I have an unusual requirement to shift an unsigned int right one bit: Dim myVar As UInt32 = UInt32.Parse("123456") Dim myResult As UInt32 myResult = myVar >> 1 However, the >> operator only works on Byte, Short, Integer, and Long.
7
7189
by: Csaba Gabor | last post by:
I'd like to detect the shift key when a button is "clicked" in Firefox/Mozilla. If the button is clicked with the mouse, no problem. However, if the onclick event is keyboard originated, then my method is not working. Same thing for SELECT elements. The simple web page below shows the issue. Click with the mouse while holding down the...
0
7695
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.