0

it is my first question, so I'm asking fot understanding. Below is function which converts a decimal number to binary. I do not understand what is happening here int bit = (weight[i] & s):

void showbits(unsigned char s)
{
    unsigned char weight[8] = { 1,2,4,8,16,32,64,128 };
    for (int = 7; i >= 0; i--)
    {
        int bit = (weight[i] & s);
        if (bit != 0)
            cout << '1';
        else cout << '0';
    }
}
François Andrieux
  • 28,148
  • 6
  • 56
  • 87
mtx8822
  • 3
  • 2

3 Answers3

1

What does int bit = (weight[i] & s) mean

T name = expression;

is syntax for variable declaration. The variable is copy initialised from the expression.

int bit = expression;

This declares the variable bit of type int.

array[index]

This is array subscripting operator. The result will be the value of the element of the array at the given index.

left & right

This is bitwise AND operation. Each bit of the result will be 1 if and only if the bit in the same position was 1 in both operands.

int bit = (weight[i] & s)

This combines all of the above. The parentheses can be used to change order of operations, but in this case they are redundant.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

This declaration

unsigned char weight[8] = { 1,2,4,8,16,32,64,128 };

sets masks for bit-wise AND operation. You can rewrite it like

unsigned char weight[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };

So this bit-wise AND expression

weight[i] & s;

checks whether the corresponding bit in the object s is set to 1.

For example if s has the value '\x65' then these operations with the masks

'\x65' & 0x01
'\x65' & 0x04
'\x65' & 0x20
'\x65' & 0x40

will yield a result that unequal to 0.

Pay attention that there is a typo in your code

for (int = 7; i >= 0; i--)

here should be

for (int i = 7; i >= 0; i--)

Here is a demonstrative program

#include <iostream>
#include <iomanip>

void showbits( unsigned char c )
{
    unsigned char weight[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };

    for ( int i = 7; i >= 0; i-- )
    {
        std::cout << ( ( weight[i] & c ) ? '1' : '0'  );
    }

    std::cout << '\n';
}

int main() 
{
    char c = 'e';

    std::cout << "\'" << c << "\' - " << std::hex << static_cast<int>( c ) << '\n';

    showbits( c );
    return 0;
}

If the system supports ASCII coding then the output will be

'e' - 65
01100101
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

In C, the & operator can have two meanings:

  1. address of operator (when used as a unary operator)
  2. bitwise AND operator (when used as a binary operator)

Additionally, in C++ (but not in C), when used when declaring a variable, it can mean that the declared variable is a reference.

In the case you describe in your question, the & operator is the bitwise AND operator.

In order to understand what is going on, I will write all values of the weight array in binary representation:

00000001 (1)
00000010 (2)
00000100 (4)
00001000 (8)
00010000 (16)
00100000 (32)
01000000 (64)
10000000 (128)

By performing the bitwise AND operation with the function parameter s together with the values listed above, a certain bit will be extracted from the variable s in every loop iteration. In the first iteration of the loop, the least-significant (i.e. lowest) bit will be extracted and printed, in the second iteration of the loop the next bit will be extracted, etc.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39