Bitwise Operator Help Pls

Started by
5 comments, last by xtr33me 17 years, 6 months ago
In school we got into the topic of bitwise operators and I was wondering if any of you have any recommmendations on ways of implementing it. If I am given a binary value and told to AND it or OR it or Shift right or left, I can do it no problem. However, I am having a rough time being able to implement it. I think my biggest problem is when it comes to implementing the bit masking. If anyone has any recommendations on something I can read it would be greatly appreciated! Thanks all in advance for the help. X
Advertisement
first of all, this belong to beginers/generic programming,please consider posting topics like this there,you will find easier the help you want.
Second answering homework-like questions is not allowed here so i will give you only some pointers.

Considering the bitwise operators in C/C++(i assume you use this language)
it's easy to implement binary masking in a given value and a given bit position.

AND &
OR |
shift right,left >> <<


example:

i want to set bit '3'(right to left) in number n,i know from boolean algebra :

a OR 1 = 1
i also know that shifting a value left by s bits is equal to value * 2^s
(easier to see like a position rather directly a value,ofcourse you can put it like a value directly if you are sure to remember 2^n n=0..32,64 etc.]

so i do : n = n OR 2^(3-1) and it's done

to C language:

n |= (1 << 2);








Virtus junxit, mors non separabit
bit masking is just using AND or OR with a mask

for example if you want to mask 10101010 with 00001111 to set the first 4 bits to 0, use 101010 AND 00001111 and you get 0001010

in what programming language do you have to do this? I'm sure most languages have operators for this.
Moving to general programming.
Thank you all for the replies. I am learning c++. Also I am not requesting answers to anything I just want something I can read to better learn on how to implement the bitwise operators. In the book we are using there are only 4 pages on bitwise operators and it isnt enough to explain the implementation.

This stuff is interesting to me, but I am not having luck finding a book locally, so I thought I would post here. Or perhaps if there is a tutorial anywhere. Thanks all again!

X
Bit-wise operations are pretty simple in C. Basically, they are the standard logical operations done on several bits in parallel. So, the AND, OR, XOR, and NOT operators work just like you expect them to (but simultaneously on several bits in parallel).

Now, as far a "bit masking" goes, here are the keys:
  • x AND 0 = 0, x AND 1 = x
  • x OR 0 = x, x OR 1 = 1
  • x XOR 0 = x, x XOR 1 = NOT x
These operations allow you to select bits to be turned off, turned on, or inverted. They also give you a way to select which bits you want to operate on.

For example, suppose you want to compute z = a AND b, but for bit 5 only (and without affecting any of the other bits in z). There are many ways to do it. Here is one way.
    mask = 0x20               // Set bit 5 to 1 and all others to 0 (for later use)             ...0100000     inverted_mask = NOT mask  // Set bit 5 to 0 and all others to 1 (for later use)             ...1011111     z5 = a AND b              // Get the results of a AND b             ...?a?????        AND ...?b?????            ==========            ...?x?????     z5 = z5 AND mask          // Since x AND 1 = x and x AND 0 = 0, bit 5 contains the result, the rest are 0.             ...?x?????        AND ...0100000            ==========            ...0x00000     z = z AND inverted_mask   // Since x AND 1 = x and x AND 0 = 0, bit 5 is 0, the rest are unchanged.             ...???????        AND ...1011111            ==========            ...?0?????     z = z OR z5               // Since x OR 0 = x, bit 5 contains the value of z5, the rest are unchanged             ...?0?????         OR ...0x00000            ==========            ...?x????? 
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Wow JohnBolton!! Thanks so much for the great resonse! Really appreciate you taking the time! This is really helpful. Thanks again.

X

This topic is closed to new replies.

Advertisement