Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Bitwise operation

Name: Anonymous 2010-06-09 5:28

How are simple operations like adding and subtraction represented using only bitwise operators such as &, |, ^, >>, ~, and <<?

Also, do each of the bitwise operators perform at the same rate, or are some faster than others?

Name: Anonymous 2010-06-09 5:31

Name: Anonymous 2010-06-09 5:50

Read SICP

Name: Anonymous 2010-06-09 5:59

Name: Anonymous 2010-06-09 7:27

>>1
In binary addition, there are 4 combinations that you can make with 0 and 1

1 + 0 = 1
0 + 1 = 1
0 + 0 = 0
1 + 1 = 10


The XOR/^ bitwise operator behaves exactly the same as the addition operator in the first 3 cases

1 XOR 0 = 1
0 XOR 1 = 1
0 XOR 0 = 0
1 XOR 1 = 0


The first 3 cases even work with bigger numbers too (it works because you don't have to carry any 1s)

   1000101010 554
 +  101000101 325
 ============
   1101101111 879

554 + 325 = 879
554 XOR 325 = 879


In our last the case (1 + 1 = 10), how can we use binary operators with 1 and 1 to get 10 and make it work with bigger numbers?  I smoked too much weed and cannot post a solution at the time.

Name: Anonymous 2010-06-09 7:28

/me yiffs all over >>3

Name: Anonymous 2010-06-09 8:07

>>4
._.

Name: Anonymous 2010-06-09 8:13

>>1
If only Google had been invented. :(

Name: Anonymous 2010-06-09 8:40

>>8
First you have to invent the Google if you want to make an apple pie from scratch.

Name: Anonymous 2010-06-09 8:45

>>8
I checked google but all it gave me was retarded java shit.  Also, see >>4

Name: Anonymous 2010-06-09 10:14

>>10
Consult Google for explanations on how to use Google effectively.
Also, who the fuck cares if you got ``retarded java shit''? Jaev's bitwise operators are exactly the same as C's.

Name: Anonymous 2010-06-09 10:46

>>11
Jaev
;)

Name: Anonymous 2010-06-09 10:58

Name: Anonymous 2010-06-09 11:02

>>11
Are there any bitwise operators that don't behave like bitwise operators?

Name: Anonymous 2010-06-09 11:03

>>14
PHP's, probably.
I don't actually know, but it's a safe bet.

Name: Anonymous 2010-06-09 11:07

>>15
$ php -r 'echo 1 ^ 2, "\n";'
3
$ php -r 'echo "1" ^ "2", "\n";'
"
$ php -r 'echo 1 ^ "2", "\n";'
3
$ php -r 'echo "1" ^ 2, "\n";'
3
$ php -r 'echo "1" ^ "2", "\n";'
"


Heh.

Name: Anonymous 2010-06-09 11:50

>>14
Perhaps, Austin Powers: smooth operator.
>>16
It should be printing a ETX char for "1" ^ "2", not sure why you are getting a ".  I'm pretty sure it performs the operator the ascii values, i.e.

$ php -r 'echo "yO-ZZV"^"V?_5=y","\n";'
/prog/

Name: Anonymous 2010-06-09 11:52


// here's my crude first attempt.
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

typedef uint8_t U;

U plus(U a, U b) {
    U m = 1, am, bm, c=0, s, r=0;
    while(m) {
        am = !!(a & m);
        bm = !!(b & m);
        s = (am ^ bm);
        if(c) {
            if(!s) {
                r |= m;
                c = am & bm;
            }
        } else {
            if(s)
                r |= m;
            c = am & bm;
        }
        printf("m:%16lu am:%lu bm:%lu c:%lu s:%lu r:%lu\n",m,am,bm,c,s,r);
        m <<= 1;
    }
    return r;
}

int main(int argc, char **argv) {
    U a=0,b=0;
    if(argv[1]) {
        a = atoi(argv[1]);
        if(argv[2]) {
            b = atoi(argv[2]);
        }
    }
    printf("%lu + %lu: %lu\n",a,b,plus(a,b));
    return 0;
}

Name: Anonymous 2010-06-09 12:52

>>17
php -r "echo 'ANUS.'^'9/\'=$';"

Name: Anonymous 2010-06-09 14:17

>>18
Did you mean:
      int add(int a, int b){
            if (!a) return b;
            else
                 return add((a & b) << 1, a ^ b);
      }

Name: Anonymous 2010-06-09 14:33

Here's some code I just saw that does a multiplication by 10 using shifts and an addition:

  x = (x << 1) + (x << 3)

Or in the original language:

  asl a
  sta $38
  asl a
  asl a
  clc
  adc $38
  sta $38

Name: Anonymous 2010-06-09 14:34

Sorry, last instruction is irrelevant.

Name: Anonymous 2010-06-09 14:43

>>21
You're trying to tell us that 2x + 8x = 10x? I don't believe you.

Name: Anonymous 2010-06-09 14:46

>>23
It's pretty amazing, I ran it over all known natural numbers in GHCi just to make sure.

Name: Anonymous 2010-06-09 14:48

I heard that adding (INT_MAX-1) to a number is like subtracting 1.

Name: Anonymous 2010-06-09 14:50

>>21
Is multiplication operator considered harmful now?

Name: Anonymous 2010-06-09 14:51

>>26
Hi, welcome to a thread on bitwise operations.

Name: Anonymous 2010-06-09 14:54

>>27
Hi, please pay your beet toll.

Name: Anonymous 2010-06-09 15:10

>>28
Poor cover, dude.

Name: Anonymous 2010-06-09 15:13

>>29
Different person.

Name: Anonymous 2010-06-09 15:16

>>30
Then >>28's opinion of >>26 is higher than merited.

Name: Anonymous 2010-06-09 15:17

>>31
I see >>26 as a poor joke that you did not `get', as it were.

Name: Anonymous 2010-06-09 15:19

>>32
I know, hence >>31. Is this discussion so hard for you to follow?

Name: Anonymous 2010-06-09 15:26

>>26
Is multiplication operator considered harmful now?
I think it's slower, it probably works like this:

  sta $4202
  lda #10
  sta $4203
  ; do something else for a while until the multiplication completes
  lda $4216

Name: Anonymous 2010-06-09 15:35

>>33
Please, leave the insults in /b/.

Name: Anonymous 2010-06-09 15:42

>>34
You're going to love finding out about optimizing compilers.

>>35
Fuck your shit.

Name: Anonymous 2010-06-09 16:14

Name: Xarn Fan !AWEsomEEEE 2010-06-09 16:24

>>37
Xarn is /prog/ and /prog/ is Xarn. If you don't like it, back to /pr/, please.

Name: Anonymous 2010-06-09 16:24

>>37
>>36 wasn't a Xarn.

Name: Anonymous 2010-06-09 16:33

BAMPU XARNAE

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List