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

Pages: 1-

hurrf durf

Name: Anonymous 2010-01-15 11:31

I suck at bit twiddling, somebody please explain why this works.

// to-binary-bitops.cpp  Print binary representation of ints
// Fred Swartz  - 2001-09-04

#include <iostream>
using namespace std;

int main() {
    int n;
    while (cin >> n) {
        cout << "decimal: " << n << endl;

        // print binary with leading zeros
        cout << "binary : ";
        for (int i=31; i>=0; i--) {
            int bit = ((n >> i) & 1);
            cout << bit;
        }
        cout << endl;
    }//end loop
    return 0;
}//end main

Name: Anonymous 2010-01-15 11:36

Do a barrel shift.

Name: Anonymous 2010-01-15 11:39

pfft google

Name: Anonymous 2010-01-15 11:43

Say you have a 4-bit number, 0101. It's ugly to explain what's going on in words alone, so here's a diagram.


>>>0 101 (n >> 3) ("0" printed)
>>01 01  (n >> 2) ("1" printed)
>010 1   (n >> 1) ("0" printed)
0101     (n >> 0) ("1" printed)
   ^


The caret sits under the first bit of the number in memory as it's shifted by a decreasing number of bits, and the & isolates the current bit by clearing the remaining bits.

Name: Anonymous 2010-01-15 11:49

You should do like >>4 is doing and work through the code by hand, since you presumably understand at least how each bitwise operator works.  Over time understanding what's going on becomes quite a bit more natural.  You're spraypainting, man.

Name: Anonymous 2010-01-15 12:17

You should use the code tags.

Name: Anonymous 2010-01-15 12:46

This is trivial.
If assume you have a number A represented by some k bits.
A = xxxxxx...x    x ∈ {0,1}
    \_______/
       k bits

The shift right operation (>> in your case, though since this is C++ and it has operator overloading, you can never know...)
would work like this:


  n1        == 1110101(2)
( n1 >> 1 ) ==  111010(2)
( n1 >> 2 ) ==   11101(2)

Now let's say you want to access(find out if it's set or not(0 or 1)) the least significant bit of a number:

A     == 10101
all you have to do is perform an AND 1 operation on A
A     == 00..0010101
1     == 00...000001
A & 1 == 00.......01

So, how do you find out the value of an arbitrary bit k?
You shift the value k bits to the right, thus getting the k-th bit as the least significant bit then do an AND 1 test. like in your code int bit = ((n >> k) & 1);
Another way would be bit = (n&(1<<k))?1:0, this just creates a number with the k-th bit set (000..01 => 000..100..k-1 times..00) and then and tests the original number. The returned value would either be 0 if it's not set or a positive value (equal to 1<<k), which you could use in an if test. Another way to do your task would be:

for(i=31;i;i--,n>>=1) printf("%d",n&1);

BTW... why not learn C first before using C++?

Name: Anonymous 2010-01-15 15:59

This is disappointing.

Name: Anonymous 2010-01-15 17:29

>>8
DISAPPOINT MY ANUS

Name: Anonymous 2010-01-15 17:38

For 64-bit:
sizeof(int) * 8 - 1
(Or 63 if you're feeling particularly faggy)

Name: Anonymous 2010-01-15 19:04

>>10
s/8/CHAR_BIT

Name: Anonymous 2010-01-15 21:35

>>11
u mena s/8/CHAR_BIT/, m i rite?

Name: Anonymous 2010-01-16 6:24

>>12
No.

Name: Anonymous 2010-11-28 10:24

Name: Anonymous 2010-12-26 6:34

Name: Anonymous 2010-12-26 12:56

Name: Anonymous 2011-02-03 1:15


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