This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-04-29 08:51:32

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Bitwise operators ?

Can we please have bitwise operators with scratch ?

They allow us to code new and high power algorithms which are extremely fast  tongue 

though they are a bit advanced but if a person has the knowledge of mod , e , ln then they should have basic knowledge of Binary ..

kindly support :-)


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#2 2012-04-29 08:56:41

Mokat
Scratcher
Registered: 2011-12-08
Posts: 1000+

Re: Bitwise operators ?

I don't know when I would use them, but I can see where it would be helpful for some people. Support  smile


http://www.eggcave.com/egg/977371.pnghttp://www.eggcave.com/egg/977376.pnghttp://www.eggcave.com/egg/1005291.pnghttp://www.eggcave.com/egg/996745.png

Offline

 

#3 2012-04-29 09:59:35

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: Bitwise operators ?

Mokat wrote:

I don't know when I would use them, but I can see where it would be helpful for some people. Support  smile

Ever heard about huffman coding ??

with bitwise operators we can logically compress our datacontaining projects or can even decode real world files in scratch :-)


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#4 2012-04-29 10:21:01

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: Bitwise operators ?

I'm not even sure what these are; care to explain?  tongue

Offline

 

#5 2012-04-29 11:24:49

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Re: Bitwise operators ?

scimonster wrote:

I'm not even sure what these are; care to explain? :P

Well, there are several

I don't know if bit shifts count, but I'll say 'em anyway.
Generally done with >> and << commands in other programming languages, these shift a number over. So say if you have 000110100101110, if you do << 2 then you'll get 011010010111000. >> goes the other way and truncates.

NOT does logical negation, so NOT 100101 is 011010. It's sometimes referred to with ~.

AND takes two numbers. If both have a one in the same place, then the result will have it. So 1101 AND 0111 will give you 0101. It's sometimes done with &, as opposed to the logical AND (&&).

OR takes two numbers. If either number has a one in a place, the result has a one in that place.  01101001 OR 10011000 will give you 11111001. Often represented by |, as opposed to the logical OR (||).

XOR, or exclusive or, takes two numbers. If only one number has a one in a place, then that place in a result will have a one. 01101 XOR 11111 equals 10010. This is commonly used for inverting an image and is often represented by ^ in programming.

Here's more explanation.

Last edited by veggieman001 (2012-04-29 11:24:55)


Posts: 20000 - Show all posts

Offline

 

#6 2012-04-29 11:30:25

joefarebrother
Scratcher
Registered: 2011-04-08
Posts: 1000+

Re: Bitwise operators ?

They are operators that operate on the binary code of the operands. There is:

() & () -- (and - AKA mask) This places a 1 in places only where both operands have a 1, 0s everywhere else.
() | () -- (or) This places a 0 in places only where both operands have a 0, 1s everywhere else.
! () -- (not) This inverts the binary code (1s where there are 0s and 0s where there are 1s)
() ^ () -- (xor) This places a 1 everywhere where the bits are different in both operands and 0 where they are the same, However, this might be confused with exponentiation.
() >> (n) -- (right shift) This deletes the first n bits from the end of the binary string
() << (n) -- (left shift) This adds n 0s on the end of the binary string

EDIT: oops, outposted by veggieman.

Last edited by joefarebrother (2012-04-30 12:05:24)


My latest project is called http://tinyurl.com/d2m8hne! It has http://tinyurl.com/d395ygk views, http://tinyurl.com/cnasmt7 love-its, and http://tinyurl.com/bwjy8xs comments.
http://tinyurl.com/756anbk   http://tinyurl.com/iplaychess

Offline

 

#7 2012-04-30 03:47:32

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: Bitwise operators ?

veggieman001 wrote:

scimonster wrote:

I'm not even sure what these are; care to explain?  tongue

Well, there are several

I don't know if bit shifts count, but I'll say 'em anyway.
Generally done with > > and < < commands in other programming languages, these shift a number over. So say if you have 000110100101110, if you do << 2 then you'll get 011010010111000. > > goes the other way and truncates.

NOT does logical negation, so NOT 100101 is 011010. It's sometimes referred to with ~.

AND takes two numbers. If both have a one in the same place, then the result will have it. So 1101 AND 0111 will give you 0101. It's sometimes done with &, as opposed to the logical AND (&&).

OR takes two numbers. If either number has a one in a place, the result has a one in that place.  01101001 OR 10011000 will give you 11111001. Often represented by |, as opposed to the logical OR (||).

XOR, or exclusive or, takes two numbers. If only one number has a one in a place, then that place in a result will have a one. 01101 XOR 11111 equals 10010. This is commonly used for inverting an image and is often represented by ^ in programming.

Here's more explanation.

Lovely post :-) +1

These are not generally used but when used these can do awesomness say for instance i will consider an mp3Frame header :-) its just 4 bytes but these 32 bits are soo nicely written that each bit has its own meaning :-) and when u wanna go to the level of handling bits , bitwise operators are the only solution you have :-)

here is a small example of there use in javascript

Code:

/* Snippet to read mp3 frame header * /
                                        var ver = ( buffer.head[1] & 0x18)>>>3;
                    var layer = ( buffer.head[1]& 0x6 )>>>1;
                    var crc = ( buffer.head[1]&0x1 );
                    var bitrate = ( buffer.head[2] & 0xF0 )>>>4 ;
                    var samplerate = ( buffer.head[2] & 0xC)>>> 2;
                    var padding = ( buffer.head[2] & 0x2)>>>1;
                    var priv = ( buffer.head[2]& 0x1 );
                    var channel = ( buffer.head[3] & 0xC0 )>>>6; 
                    var jsExt =  ( buffer.head[3]& 0x30)>>> 4; /* joing stereo ext */
                    var cpyrite = ( buffer.head[3]& 0x9)>>> 3;
                    var orig = ( buffer.head[3] & 0x4 )>>>2;
                    var Emph = ( buffer.head[3] & 0x3 );

Now each element in buffer has like 1 byte of data but you can see how much data are we extracting from just 3 bytes of data :-) makes sense ?? It can open a whole new paths for programmers using scratch ,

moreover  tongue  they are extremely fast  tongue

Last edited by fanofcena (2012-04-30 03:50:39)


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#8 2012-04-30 03:49:42

trinary
Scratcher
Registered: 2012-01-29
Posts: 1000+

Re: Bitwise operators ?

It would be nice, but Scratch...is for learning to program.
It's primarily for beginners.
It may be confusing for them.

However, I still support.

Last edited by trinary (2012-04-30 03:50:03)


http://trinary.tk/images/signature_.php

Offline

 

#9 2012-04-30 03:55:15

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: Bitwise operators ?

trinary wrote:

It would be nice, but Scratch...is for learning to program.
It's primarily for beginners.
It may be confusing for them.

However, I still support.

Well scratch has a lot of things in the arithimetics secion which can confuse :-) , and by the way language without bitwise operators cannot be called a real programming language now can it ?


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

Board footer