Help: C char bits operations to python converting

Arpad Kiss sekter at matavnet.hu
Wed Sep 27 11:13:18 EDT 2000


<gangli at msn.com> wrote in message news:8qt0fd$ucv$1 at nnrp1.deja.com...
> In article <8qst1l$rbj$1 at nnrp1.deja.com>,
>   fredrik at pythonware.com wrote:
> > gangli at msn.com wrote:
> > > > >How could I convert following C statments to Python?
> > > > >   char tmp_ch, tmp_ch0, tmp_ch1;
> > > > >   tmp_ch = 'K' - 'A';          /* -> 10 */
> > > > >   tmp_ch0 = ('N' - 'A') << 4;  /* -> -48 */
> > > > >   tmp_ch1 = tmp_ch+ tmp_ch0;   /* -> -38 */
> > > >
> > > > Use the built-in ord() function.
> > >
> > > It only help convert "tmp_ch = 'K' - 'A';", but not others
> >
> > it sure does.  try again.
> >
>
> It does not:
> >>> (ord('N') - ord('A')) << 4
> 208
>
In Python (ord('N') - ord('A')) is represented as integer(at least 32bits)
so you shift at least 32bits in Python and not 8bits.
Otherwise I think converting C programs line by line is not the best method,
because programming in Python is completly different than programming in C.
Try to rewrite your algorithm in a Pythonist way, I am sure this would
produce a more elegent solution than your tricky C implemetation.
Arpad






More information about the Python-list mailing list