Discussion:
Bitwise Shift in Forth?
(too old to reply)
Erik Hentell
2004-04-13 23:51:14 UTC
Permalink
Hi,

I'm going over some C code to see if I can rewrite some of it in Mops
(an exercise to learn how the language works). Things were going well
for(w = 0; w < myRect.right; w++, myPixelPtr++){
UInt8 r, g, b, a;
long myPixel= *myPixelPtr;
if ((myPixel & 0x0ffffff) == 0x00ffffff) //pure white
continue;
a = (myPixel >> 24) & 0x0ff;
r = (myPixel >> 16) & 0x0ff;
g = (myPixel >> 8) & 0x0ff;
b = (myPixel >> 0) & 0x0ff;
if ((r > kThreshold) && (g > kThreshold) && (b > kThreshold) {
r = g = b = kThreshold;
*myPixelPtr = (a << 24) | (kThreshold << 16) | (kThreshold << 8)
| (kThreshold << 0);
}
}
It's a for loop that is part of a function which runs through an image
and changes values depending on how close they are to white. The "&"
and "|" are of course bitwise operators, which have their equivalents
as "and" and "or" in Mops.

The question I have is this: what is the Mops equivalent to "<<" and
">>"? I haven't found anything on the web in general regarding Forth
and bitwise shift operators, and I'm not sure where to look in the
manual. If someone could point me in the right direction, I'd
appreciate it.

Thanks,
Erik
Doug Hoffman
2004-04-14 09:28:24 UTC
Permalink
Erik,
Post by Erik Hentell
( n1 n2 -- n2 )
Arithmetic Nuc2.asm/pnuc1
Shifts n1 to the right by n2 bit positions. e.g. 12 2 >> will yield 3
(1100) -> (0011).

<< ( n1 n2 -- n3 )
Arithmetic Nuc2.asm/pnuc1
Shifts n1 to the left by n2 bit positions. e.g. 3 2 << will yield 12
(0011) -> (1100).


Hope this is what you wanted.

Regards,

-Doug
Post by Erik Hentell
Hi,
I'm going over some C code to see if I can rewrite some of it in Mops
(an exercise to learn how the language works). Things were going well
for(w = 0; w < myRect.right; w++, myPixelPtr++){
UInt8 r, g, b, a;
long myPixel= *myPixelPtr;
if ((myPixel & 0x0ffffff) == 0x00ffffff) //pure white
continue;
a = (myPixel >> 24) & 0x0ff;
r = (myPixel >> 16) & 0x0ff;
g = (myPixel >> 8) & 0x0ff;
b = (myPixel >> 0) & 0x0ff;
if ((r > kThreshold) && (g > kThreshold) && (b > kThreshold) {
r = g = b = kThreshold;
*myPixelPtr = (a << 24) | (kThreshold << 16) | (kThreshold << 8)
| (kThreshold << 0);
}
}
It's a for loop that is part of a function which runs through an image
and changes values depending on how close they are to white. The "&"
and "|" are of course bitwise operators, which have their equivalents
as "and" and "or" in Mops.
The question I have is this: what is the Mops equivalent to "<<" and
">>"? I haven't found anything on the web in general regarding Forth
and bitwise shift operators, and I'm not sure where to look in the
manual. If someone could point me in the right direction, I'd
appreciate it.
Thanks,
Erik
Erik Hentell
2004-04-14 17:42:53 UTC
Permalink
Holy cow! I went up and down the Mops manual and must have gone right
by it! Sorry about that...

Erik
Post by Doug Hoffman
Erik,
( n1 n2 -- n2 )
Arithmetic Nuc2.asm/pnuc1
Shifts n1 to the right by n2 bit positions. e.g. 12 2 >> will yield 3
(1100) -> (0011).
<< ( n1 n2 -- n3 )
Arithmetic Nuc2.asm/pnuc1
Shifts n1 to the left by n2 bit positions. e.g. 3 2 << will yield 12
(0011) -> (1100).
Hope this is what you wanted.
Regards,
-Doug
Doug Hoffman
2004-04-15 09:08:57 UTC
Permalink
No apology necessary. I don't thnk it's in the manual. It's because of
words like this that we created the Subject Glossary folder with files
organized by subject. In this case you would find these words in the
Subject Glossary file Arithmetic (in addition to the online glossary).
Although the Subject Glossary is in need of updating. I see that it does
not contain A>> (arithmetic shift, or signed shift).

Regards,

-Doug
Post by Erik Hentell
Holy cow! I went up and down the Mops manual and must have gone right
by it! Sorry about that...
Erik
Post by Doug Hoffman
Erik,
( n1 n2 -- n2 )
Arithmetic Nuc2.asm/pnuc1
Shifts n1 to the right by n2 bit positions. e.g. 12 2 >> will yield 3
(1100) -> (0011).
<< ( n1 n2 -- n3 )
Arithmetic Nuc2.asm/pnuc1
Shifts n1 to the left by n2 bit positions. e.g. 3 2 << will yield 12
(0011) -> (1100).
Hope this is what you wanted.
Regards,
-Doug
Doug Hoffman
2004-04-15 09:29:27 UTC
Permalink
While on the topic of looking for words in Mops it is probably worthwhile to
mention the following:

1) The Manual is of course an excellent reference.

2) The Subject Glossary is a good reference, though a bit dated.

3) Using "wordsWith" is a great way to track down those obscure words.
Usage in this case would be wordsWith >>

need zstring+
string+ s1
string+ s2

: .id ( xt -- )
NAME? IF >name N>COUNT ( addr len )
put: s2
get: s1 search: s2
IF all: s2 type cr 0 -> out THEN
ELSE drop
THEN ;

: doWith ( addr len -- )
new: s1 new: s2 put: s1
setToTop: theMark
0 -> out cr
BEGIN
next: theMark
?dup
WHILE
link> .id
REPEAT
release: s1 release: s2 ;

: wordsWith
bl word count ( addr len )
doWith
cr ." wordsWith done " cr
;


4) Once a word is found or if you are just guessing either the online
glossary (available in Quick Edit) or a locate (command-= in QE) are good
ways to view the word definition (or see if it exists) and its source code.

5) Don't forget the source browser control-click for class names and method
names and the source browser control-option-click for colon definitions.
Both available in QE.

-Doug

Continue reading on narkive:
Loading...