### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Bitwise operation problem  (Read 922 times)

#### sblisesivdin

• New Member
• Posts: 13
##### Bitwise operation problem
« on: May 26, 2019, 12:51:06 pm »
Hello all,
I am trying to replicate the python bitwise operation:
Code: Python  [Select][+][-]
1. resultinteger = (inputinteger >> 24) & 0xff
with
Code: Pascal  [Select][+][-]
1. resultinteger = (strtoint(Edit1.Text) shr 24) And \$FF;

however, I am getting an "illegal expression" error.  I think the problem lies in using And with \$255. But I do not know how to do.

« Last Edit: May 26, 2019, 12:56:59 pm by sblisesivdin »

#### sblisesivdin

• New Member
• Posts: 13
##### Re: Bitwise operation problem
« Reply #1 on: May 26, 2019, 01:00:30 pm »
It was just a "=" -> ":=" thing. Sorry for any inconvenience.

#### howardpc

• Hero Member
• Posts: 3490
##### Re: Bitwise operation problem
« Reply #2 on: May 26, 2019, 01:37:55 pm »
Don't forget to provide for the possibility that Edit1.Text is blank.

#### jamie

• Hero Member
• Posts: 3278
##### Re: Bitwise operation problem
« Reply #3 on: May 26, 2019, 04:22:37 pm »
maybe StrToIntDef(Edit1.text, 0) could be used ?
The only true wisdom is knowing you know nothing

#### ASerge

• Hero Member
• Posts: 1644
##### Re: Bitwise operation problem
« Reply #4 on: May 26, 2019, 04:57:24 pm »
maybe StrToIntDef(Edit1.text, 0) could be used ?
For me, better an exception than zero comes.

#### jamie

• Hero Member
• Posts: 3278
##### Re: Bitwise operation problem
« Reply #5 on: May 26, 2019, 05:07:01 pm »
True but,,

Lets say there are defaults for edit fields if the user decides not to fill them in...

MyValue := StrIoIntDef(Edit1.Text, SomeDefaultValue);
Edit1.Text := IntTostr(MyValue);

This would in turn fill in the default value..

In places where there are no defaults and use must supply something, then I would say a check point is in hand but I
don't like using  Exception blocks , I like check points in code.
The only true wisdom is knowing you know nothing

#### ASerge

• Hero Member
• Posts: 1644
##### Re: Bitwise operation problem
« Reply #6 on: May 26, 2019, 05:22:44 pm »
Lets say there are defaults for edit fields if the user decides not to fill them in...
If there is a default value, it is better to insert it into the edit. And if the user has not explicitly specified a value, then the message that ["" is invalid integer] is a normal reaction.

#### lucamar

• Hero Member
• Posts: 2975
##### Re: Bitwise operation problem
« Reply #7 on: May 26, 2019, 05:30:33 pm »
In places where there are no defaults and use must supply something, then I would say a check point is in hand but I don't like using  Exception blocks , I like check points in code.

For those cases, one can use TryStrToInt(). Mightly handy family of functions, those TrySomeTypeToOtherType
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

#### Zoran

• Hero Member
• Posts: 1575
##### Re: Bitwise operation problem
« Reply #8 on: May 26, 2019, 06:03:50 pm »

Howard, Jamie, ASerge, Lucamar,
Didn't you notice what it actually was about?

It was just a "=" -> ":=" thing. Sorry for any inconvenience.

#### howardpc

• Hero Member
• Posts: 3490
##### Re: Bitwise operation problem
« Reply #9 on: May 26, 2019, 08:58:20 pm »
Yes, I noticed, and felt that a related issue was worth a mention.

#### lucamar

• Hero Member
• Posts: 2975
##### Re: Bitwise operation problem
« Reply #10 on: May 26, 2019, 09:19:38 pm »
Yes, I noticed too. My answer, as the ones above it, is more related to Howard's:

Don't forget to provide for the possibility that Edit1.Text is blank.

and inmediately related, of course (as you can see from the quote) to jamie's.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

#### Zoran

• Hero Member
• Posts: 1575
##### Re: Bitwise operation problem
« Reply #11 on: May 26, 2019, 09:33:54 pm »
Ok, sorry.

#### Zoran

• Hero Member
• Posts: 1575
##### Re: Bitwise operation problem
« Reply #12 on: May 26, 2019, 09:50:38 pm »
Then, my contribution to the discussion:
Code: Pascal  [Select][+][-]
1. var
2.   N: Integer;
3. ...
4.   if not TryStrToInt(Edit1.Text, N) then
5.     MessageDlg('Please put an integer in the edit box', mtError, [mbClose], 0)
6.   else
7.     resultinteger := (N shr 24) and \$FF;

Also notice that, "and \$FF" is actually unnecessary, as result of (Try)StrToInt is 32-bit integer, so after shifting 24 bits to right, already only right-most 8 bits can be non-zeros.
« Last Edit: May 26, 2019, 09:58:50 pm by Zoran »