To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.roboticsOpen lugnet.robotics in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / 22641
22640  |  22642
Subject: 
bitwise manipulations in C
Newsgroups: 
lugnet.robotics
Date: 
Wed, 11 Aug 2004 23:36:58 GMT
Viewed: 
1208 times
  
Hi,
Can anybody tell me about bitwise manipulations or point me to a site
that explains what it is, what its used for, and how to use it?

thanks,
Scott

Hi Scott,

please regard this as an extension to explainations from Steve Baker:

1. the code

Binary Decimal Hexadecimal
0000     0       0
0001     1       1
0010     2       2
0011     3       3
0100     4       4
0101     5       5
0110     6       6
0111     7       7
1000     8       8
1001     9       9
1010    10       A
1011    11       B
1100    12       C
1101    13       D
1110    14       E
1111    15       F


2. well known programming technique

bitwise manipulations origin from assembler programming. a processor
allows to execute commands, that perform it directly. High level
languages like NQC use those processor commands from their high level.
respectively

Note: from the assembler programming point of view, variables in, say
NQC, are a 16-Bit bunch of bits, as steve states. if you use bitwise
manipulations in NQC, you should regard them as same, not as signed
integers.

bitwise manipulations operate on a variable holding the data bits,
manipulations are done on. And a bitmask, selecting desired bits. Let
us see, how bitmasks are organized.


2.1 ANDing bits ( using AND bitmask )

2.1.1 gettig the masked bits

using an AND bitmask results in gettig the masked bits, if they are
ones (1) in the bit mask y. unmasked bits, holding a zero (0) are
preserved.

Lets us see an example. You have a 16 bit variable named x and a
bitmask named y. The bitmask 'gets' the bits from the variable, where
it has a 1 set in a bit position. The other bits are cleared (masked
out).

Nibble Nr   3    2    1    0

      x   0110 1010 0011 1111
      y   0000 0000 1111 0000
      -----------------------
      z   0000 0000 0011 0000

z is the result of ANDing the bitmask y with the content of the
variable x. you can downshift z to get the number of 3 in a resulting
NQC variable. Note: in this NQC alike shift command zeros are filled in from
the left to z ( see NQC example ).


2.1.2 clearing desired bits

Nibble Nr   3    2    1    0

      x   0110 1010 0011 1111
      y   1111 1111 0000 1111
      -----------------------
      z   0110 1010 0000 1111


2.2 ORing bits ( using OR bitmask )

using an OR bitmask results in setting the masked bits. The states of
the other bits are saved in the result:

Nibble Nr   3    2    1    0

      x   0110 1010 0011 1111
      y   0000 0000 1111 0000
      -----------------------
      z   0110 1010 1111 1111


2.3 XNORing bits ( using XNOR bitmask )

Big trick now is how to invert bits. We dont use the XOR function, but
the NOT XOR function here, called XNOR function. Lets see the
exclusive nor truth table at first.

     yx z   x=NQC variable bit y=mask bit  z=result bit x xnor y
     00 1
     01 0
     10 0
     11 1                         bit operation

You see, that if y is 0 z is inverted x. And if y is 1, z is equal x.

Nibble Nr   3    2    1    0

      x   0110 1010 0011 1111
      y   0000 0000 1111 0000
      -----------------------
      z   1001 0101 0011 0000


2.4 Testing bits

after we had done basic logic functions on bunches of bits, we might
be interested in testing specific bits, to specify programming
execution path. Use the AND bitwise manipulation to do it.

Nibble Nr   3    2    1    0

      x   0110 1010 0011 1111       data bits
      y   0000 0000 0001 0000       AND bitmask selecting desired bit
      -----------------------
      z   0000 0000 0001 0000       a non zero result

Nibble Nr   3    2    1    0

      x   0110 1010 0010 1111       data bits
      y   0000 0000 0001 0000       AND bitmask selecting desired bit
      -----------------------
      z   0000 0000 0000 0000       a zero result

The y mask specifies the tested bit in the example above. Note: if the
complete result is zero, the requested masked bit is not set in the
data.

Remeber C boolean condition (in if statements, for example? ) ?


NQC Code Example:
-----------------

NQC "numeric constants can be written as either decimal (e.g. 123) or
hexadecimal (e.g. 0xABC)" as Dave Baum states in his "NQC Programmer's
Guide".

2.1.1 gettig the masked bits

nibble   3 2 1 0

      x   6 A 3 F
      y   0 0 F 0
      ----------- Hexadecimal Code, AND bitwise function
      z   0 0 3 0

      var x, y, z;
      x = 0x6A3F; /* setup data */
      y = 0x00F0; /* setup nibble 1 selection mask */
      z = x & y;  /* get data from nibble 1 by clearing others out */
      /* z holds 0x0030 now */
      /* next, we shift z 4 bits to the right, to make nibble 1
         available as an integer value in the z variable */
      z = z >> 4;
      /* z holds value of 3=0x0003 now */

Note, you have to code binary data to hexadecimal (which is much more
convient than using decimal), to use it in NQC. In C, you could code
it in binary, directly. But this is not a limitation, about what you
can do in NQC. Only a coding issue.

There is no limitation to creativity. The list of what you might
archive with bitwise manipulations is, by nature, not complete here.
Only the basic functions are shown. A bitmask is not allways in the
game. If, for example, you simply OR two variables together, you get
ones (1) in the result, where ones are set in either input variable.
So be creative.

Do you have some more entertaining questions?

Greetings
Ralph



1 Message in This Thread:

Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR