views:

1345

answers:

10

Hi,

I am trying to learn C programming, and I was studying some source codes and there are some things I didn't understand, especially regarding Bitwise Operators. I read some sites on this, and I kinda got an idea on what they do, but when I went back to look at this codes, I could not understand why and how where they used.

My first question is not related to bitwise operators but rather some ascii magic:

  1. Can somebody explain to me how the following code works?

    char a = 3;
    int x = a - '0';
    

    I undertand this is done to convert a char into an int, however I don't understand the logic behind it. Why/How does it work?

  2. Now, Regarding Bitwise operators, I feel really lost here.

    • What does this code do?

      if (~pointer->intX & (1 << i)) { c++; n = i; }
      

      I read somewhere that ~ inverts bits, but I fail to see what this statement is doing and why is it doing that.

      Same with this line:

      row.data = ~(1 << i);
      
    • Other question:

      if (x != a)
        {
          ret |= ROW;
        }
      

      What exacly is the |= operator doing? From what I read, |= is OR but i don't quite understand what is this statement doing.

      Is there any way of rewriting this code to make it easier to understands so that it doesn't use this bitwise operators? I find them very confusing to understand, so hopefully somebody will point me in the right direction on understanding how they work better!

Thanks a lot in advance for taking your taking to respond to this !


Thank you very much to everyone for your excellent responses ! I have a much better understanding of bitwise operators now and the whole code makes much more sense now. I really appreciate your help and time!

One last thing: appartenly nobody responded if there would be a "cleaner" way for rewriting this code in a way that its easier to understand and maybe not at "bitlevel". Any ideas?

Again, thank you all very much!!

A: 

ret |= ROW;

is equivalent to

ret = ret | ROW;

Albert
A: 

Single quotes are used to indicate that a single char is used. '0' therefore is the char '0', which has the ASCII-Code 48. 3-'0'=3-48

'1<<i' shifts 1 i places to the left, therefore only the ith bit from the right is 1.
~pointer->intX negates the field intX, so the logical AND returns a true value (not 0) when intX has every bit except for the ith bit from the right isn't set.

tstenner
A: 
char a = '3';  
int x = a - '0';

you had a typo here (notice the 's around the 3), this assigns the ascii value of the character 3, to the char variable, then the next line takes '3' - '0' and assigns it to x, because of the way ascii values work, x will then be equal to 3 (integer value)

In the first comparison, I've never seen ~ being used on a pointer that way before, another typo maybe? If I were to read out the following code:

(~pointer->intX & (1 << i))

I would say "(the value intX dereferenced from pointer) AND (1 left shifted i times)"

1 << i is a quick way of multiplying 1 by a power of 2, ie if i is 3, then 1 << 3 == 8

In this case, I have no clue why you would invert the bits of the pointer..

In the 2nd comparison, x |= y is the same as x = x | y

Josh W.
-> has a higher precedence than ~, you can read that as ~(pointer->intX)
Ferruccio
A: 

For char a = 3; int x = a - '0'; I think you meant char a = '3'; int x = a - '0';. It's easy enough to understand if you realize that in ASCII the numbers come in order, like '0', '1', '2', ... So if '0' is 48 and '1' is 49, then '1' - '0' is 1.

For bitwise operations, they are hard to grasp until you start looking at bits. When you view these operations on binary numbers then you can see exactly how they work...

010 & 111 = 010
010 | 111 = 111
010 ^ 111 = 101
~010 = 101
dwc
A: 

I think you probably have a typo, and meant:

char a = '3';

The reason this works is that all the numbers come in order, and '0' is the first. Obviously, '0' - '0' = 0. '1' - '0' = 1, since the character value for '1' is one greater than the character value for '0'. Etc.

Mark Ransom
+15  A: 

This will produce junk:

char a = 3; 
int x = a - '0';

This is different - note the quotes:

char a = '3'; 
int x = a - '0';

The char datatype stores a number that identifiers a character. The characters for the digits 0 through 9 are all next to each other in the character code list, so if you subtract the code for '0' from the code for '9', you get the answer 9. So this will turn a digit character code into the integer value of the digit.

(~pointer->intX & (1 << i))

That will be interpreted by the if statement as true if it's non-zero. There are three different bitwise operators being used.

The ~ operator flips all the bits in the number, so if pointer->intX was 01101010, then ~pointer->intX will be 10010101. (Note that throughout, I'm illustrating the contents of a byte. If it was a 32-bit integer, I'd have to write 32 digits of 1s and 0s).

The & operator combines two numbers into one number, by dealing with each bit separately. The resulting bit is only 1 if both the input bits are 1. So if the left side is 00101001 and the right side is 00001011, the result will be 00001001.

Finally, << means left shift. If you start with 00000001 and left shift it by three places, you'll have 00001000. So the expression (1 << i) produces a value where bit i is switched on, and the others are all switch off.

Putting it all together, it tests if bit i is switched off (zero) in pointer->intX.

So you may be able to figure out what ~(1 << i) does. If i is 4, the thing in brackets will be 00010000, and so the whole thing will be 11101111.

ret |= ROW;

That one is equivalent to:

ret = ret | ROW;

The | operator is like & except that the resulting bit is 1 if either of the input bits is 1. So if ret is 00100000 and ROW is 00000010, the result will be 00100010.

Daniel Earwicker
It also checks whether every other bit is 0.
tstenner
@tstenner - which part are you referring to?
Daniel Earwicker
+1 got a nice answer. i think small typo here: "if i is 5, the thing.." s,5,4,g
Johannes Schaub - litb
@litb - thanks, fixed.
Daniel Earwicker
+1 for not automatically assuming ASCII. The '0'...'9' relation is guaranteed by C, while ASCII is not.
David Thornley
The joys of http://www.faqs.org/docs/jargon/E/EBCDIC.html
Daniel Earwicker
A: 

1) A char is really just a 8-bit integer. '0' == 48, and all that that implies.

2) (~(pointer->intX) & (1 << i)) evalutates whether the 'i'th bit (from the right) in the intX member of whatever pointer points to is not set. The ~ inverts the bits, so all the 0s become 1s and vice versa, then the 1 << i puts a single 1 in the desired location, & combines the two values so that only the desired bit is kept, and the whole thing evalutes to true if that bit was 0 to begin with.

3) | is bitwise or. It takes each bit in both operands and performs a logical OR, producing a result where each bit is set if either operand had that bit set. 0b11000000 | 0b00000011 == 0b11000011. |= is an assignment operator, in the same way that a+=b means a=a+b, a|=b means a=a|b.

Not using bitwise operators CAN make things easier to read in some cases, but it will usually also make your code significantly slower without strong compiler optimization.

Sparr
Bear in mind that C doesn't mandate that '0' == 48, although that's what it is in ASCII. C mandates that the digit characters are in consecutive order from '0' to '9'.
David Thornley
An excellent point. Let me know when EBCDIC comes back into style :)
Sparr
A: 

The subtraction trick you reference works because ASCII numbers are arranged in ascending order, starting with zero. So if ASCII '0' is a value of 48 (and it is), then '1' is a value of 49, '2' is 50, etc. Therefore ASCII('1') - ASCII('0') = 49 - 48 = 1.

As far as bitwise operators go, they allow you to perform bit-level operations on variables.

Let's break down your example:

(1 << i) -- this is left-shifting the constant 1 by i bits. So if i=0, the result is decimal 1. If i = 1, it shifts the bit one to the left, backfilling with zeros, yielding binary 0010, or decimal 2. If i = 2, you shift the bit two to the left, backfilling with zeros, yielding binary 0100 or decimal 4, etc.

~pointer->intX -- this is taking the value of the intX member of pointer and inverting its bits, setting all zeros to ones and vice versa.

& -- the ampersand operator does a bitwise AND comparison. The results of this will be 1 wherever both the left and right side of the expression are 1, and 0 otherwise.

So the test will succeed if pointer->intX has a 0 bit at the ith position from the right.

Also, |= means to do a bitwise OR comparison and assign the result to the left side of the expression. The result of a bitwise OR is 1 for every bit where the corresponding left or right side bit is 1,

mwigdahl
A: 

1) Can somebody explain to me how the following code works? char a = 3; int x = a - '0'; I undertand this is done to convert a char into an int, however I don't understand the logic behind it. Why/How does it work?

Sure. variable a is of type char, and by putting single quotes around 0 that is causing C to view it as a char as well. Finally, the whole statement is automagically typecast to its integer equivalent, because x is defined as an integer.

2) Now, Regarding Bitwise operators, I feel really lost here. --- What does this code do? if (~pointer->intX & (1 << i)) { c++; n = i; } I read somewhere that ~ inverts bits, but I fail to see what this statement is doing and why is it doing that.

(~pointer->intX & (1 << i)) is saying:

negate intX, and AND it with a 1 shifted left by i bits

so, what you're getting, if intX = 1011, and i = 2, equates to

(0100 & 0100) 

-negate 1011 = 0100

-(1 << 2) = 0100

0100 & 0100 = 1 :)

then, if the AND operation returns a 1 (which, in my example, it does) { c++; n = i; }

so, increment c by 1, and set variable n to be i

Same with this line: row.data = ~(1 << i);

Same principle here.
Shift a 1 to the left by i places, and negate.

So, if i = 2 again

(1 << 2) = 0100

~(0100) = 1011

**--- Other question:

if (x != a) { ret |= ROW; }

What exacly is the |= operator doing? From what I read, |= is OR but i don't quite understand what is this statement doing.**

if (x != a) (hopefully this is apparent to you....if variable x does not equal variable a)

ret |= ROW;

equates to

ret = ret | ROW;

which means, binary OR ret with ROW

For examples of exactly what AND and OR operations accomplish, you should have a decent understanding of binary logic.

Check wikipedia for truth tables...ie

Bitwise operations

espais
A: 
  1. I'm assuming you mean char a='3'; for the first line of code (otherwise you get a rather strange answer). The basic principal is that ASCII codes for digits are sequential, i.e. the code for '0'=48, the code for '1'=49, and so on. Subtracting '0' simply converts from the ASCII code to the actual digit, so e.g. '3' - '0' = 3, and so on. Note that this will only work if the character you're subtracting '0' from is an actual digit - otherwise the result will have little meaning.

  2. a. Without context the "why" of this code is impossible to say. As for what it's doing, it appears that the if statement evaluates as true when bit i of pointer->intX is not set, i.e. that particular bit is a 0. I believe the & operator gets executed before the ~ operator, as the ~ operator has very low precedence. The code could make better use of parentheses to make the intended order of operations clearer. In this case, the order of operations might not matter though - I believe the result is the same either way.

    b. This is simply creating a number with all bits EXCEPT bit i set to 1. A convenient way of creating a mask for bit i is to use the expression (1<<i).

  3. The bitwise OR operation in this case is used to set the bits specified by the ROW constant to 1. If these bits are not set, it sets them; if they're already set it has no effect.