5 Replies - 1892 Views - Last Post: 14 August 2011 - 01:00 PM Rate Topic: -----

#1 insanepenguin  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 238
  • Joined: 08-July 10

Bitwise operation to determine bit

Posted 14 August 2011 - 10:46 AM

Hi guys, I've started a chapter on indexers and was wondering if you could explain this expression a bit :)

(bits & (1 << 6)) != 0


'bits' is an integer and I want to check the binary digit at position 6.

Why does it use binary 1: 00000001 to shift across?

Many thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Bitwise operation to determine bit

#2 [email protected]  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Bitwise operation to determine bit

Posted 14 August 2011 - 11:37 AM

*
POPULAR

I'll try an explain this as best I can :)

I'll just clarify that when I say 'result' or 'resulting number', I am referring to the binary number resulting from an & operation, i.e. the number resulting from (bits & (1 << 6))

'bits' is the first operand, 1 << 6 is the second operand, and we are applying & to get the 'resulting number', in my terminology :)



Right, as you know, binary 1 is:

Quote

00000001


1 << 6 equals:

Quote

01000000


So, the '1' gets shifted to position 6.

The key to understanding this is understanding how the & operator works. Basically, given two binary numbers, when & is applied to them, all positions with a 1 in both numbers/operands get a 1 in the resulting number, and every other position gets a 0.

Some examples:

  00001000 
& 01010101

= 00000000    as none of the positions (0,1,2,3,4,5,6 or 7) are set to 1 in BOTH numbers


  01000000
& 01000000

= 01000000  the 1 is there in position 6, as position 6 in both numbers contains a 1



The key to understanding your expression is that, when applying & to two binary numbers, if a position has a 0 in EITHER of the numbers, that position in the resulting number will ALWAYS contain a 0 (as, for a 1 to appear in the resulting number in a position, that position in BOTH numbers/operands must be a 1).


So, back to your expression. Say bits equals 100, which in binary, is 01100100. We want to check whether the number at position 6 is a 1 or a 0 (note that the bit in position 6 IS set (i.e. it equals 1).

  01000000      (1 << 6)  
& 01100100      ('bits')

= 01000000  != 0  ('resulting number')



We use 1, in the expression 1 << 6, so that position 6 is 1 (01000000), and ALL the other positions are 0.

This is vital as we have established that because of how & works, this means that positions 0,1,2,3,4,5 and 7 will ALWAYS be 0 in the resulting number (see the bold paragraph above for the reason why), as those positions in the resulting number will always be 0. Further, position 6 in the result of 1 << 6 is 1, so position 6 in the resulting number will be 1 IF position 6 in 'bits' is 1, or 0 if position 6 in 'bits' is 0. .

So, the up shot of it is, if the bit in position 6 in 'bits' is 0, the resulting number will be:

00000000 (== 0) bit 6 not set

if it is 1, it will be:

01000000 (!= 0) bit 6 set

When applying & to two numbers, it may help to think of 1 as true, and 0 as false, and imagine you are applying the && operator to the true and false values in matching positions in the two numbers.

so, 1 << 6 becomes:

  0     1    0     0     0     0     0     0
false true false false false false false false


Feel free to ask for clarification, as it isn't the easiest thing to understand when you first meet it (or indeed explain after you've met it ;))

This post has been edited by [email protected]: 14 August 2011 - 12:02 PM

Was This Post Helpful? 6
  • +
  • -

#3 insanepenguin  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 238
  • Joined: 08-July 10

Re: Bitwise operation to determine bit

Posted 14 August 2011 - 12:15 PM

Thanks a lot mate that really helped

So just to clarify with my own example!

bits = 42
00101010

and I want position 4
1 << 4

00001000 - this sorta isolates the bit in question(4th)?

bits & (1 << 4)

00101010
00001000

When & is applied to these two you get
00001000 so in this case position 4 = 1?
Was This Post Helpful? 0
  • +
  • -

#4 [email protected]  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Bitwise operation to determine bit

Posted 14 August 2011 - 12:22 PM

I'd say you've understood that perfectly, and this:

Quote

00001000 - this sorta isolates the bit in question(4th)?


I think the use of the word 'isolates' is very fitting. That is really why we use 1 in the 1 << 6 expression, to 'isolate' the bit in question!

I think you've made a small error in your example in that 1 << 4 is 00010000. Bit positions are like positions in an array. The first bit is at position 0, the second is at position 1 etc, so the bit at position 4 is actually the 5th bit.

Although, that's a very simple error that's easy to make when typing a load of successive 0's and 1's :) I have no doubt from what you said there that you have understood what I explained fully.

This post has been edited by [email protected]: 14 August 2011 - 12:45 PM

Was This Post Helpful? 0
  • +
  • -

#5 insanepenguin  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 238
  • Joined: 08-July 10

Re: Bitwise operation to determine bit

Posted 14 August 2011 - 12:46 PM

Pesky N-1 rule! I've only just got on to indexers, are they a way to simplify code so you don't have to write long or complicated expressions every time you need to access something in a collection by placing said expressions in a class or struct?
Was This Post Helpful? 0
  • +
  • -

#6 [email protected]  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Bitwise operation to determine bit

Posted 14 August 2011 - 01:00 PM

Essentially yes. Indexers really just provide convenient syntax that allow classes and structs to be treated as though they were arrays , by using a familiar index system to access different values. This makes for a more intuitive and simple interface for clients that use the class/struct to interact with, and makes the intent of the class (usually the intent of such a class is to wrap an internal collection) clearer.

Note arrays are 0 based (i.e. element 1 is at index 0), but if you implement your own indexer in a class that wraps an array, you could make it so, as far as clients are concerned, element 1 is at position 1, element 2 is at position 2. It's not a particularly great idea to do so, but you could if you wanted. (I would get into the logical reasoning (to me at least) why arrays start at 0, but that's deviating from the topic too much :))

I suppose indexers also allow an easy way for classes to perform simple validation on incoming data before they get put into an internal collection, much like properties do.

You can read about their usage here

For fear of moving too far off topic (before we move too far into this), you should probably make another topic if you have questions about indexers, as we are talking about two entirely different topics now :)

This post has been edited by [email protected]: 14 August 2011 - 01:10 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1