Page 1 of 1 [ 4 posts ] 

Madbones
Veteran
Veteran

User avatar

Joined: 7 Mar 2010
Age: 27
Gender: Male
Posts: 777
Location: In the zone

15 Jul 2011, 5:24 pm

Hey.
I want to learn about computer bits.
Like status bits and modified bits kind of a thing.
Where online can I learn about these for free.
No particular reason why I want to learn, but I'm pretty interested.



mcg
Veteran
Veteran

User avatar

Joined: 26 Jan 2010
Age: 34
Gender: Male
Posts: 538
Location: Sacramento

15 Jul 2011, 8:04 pm

A bit is just an eighth of a byte. Which bits serve what purpose is implementation specific and should be included in the documentation for whatever platform with which you are working (you may want to take a look at the IA-32 manuals from Intel for some examples, x86's have several control and status registers).

I assume you want to learn about bit manipulation techniques (how to set a bit, test a bit, clear a bit, etc).

There's really nothing to it, you basically just use an appropriate bitwise operator and bitmask for your situation.

For example, if you have a a byte, and you want to set bit 5 (I am referring to bit 0 as the least significant bit and bit 7 as the most significant bit) while leaving the other bits unchanged, you simply OR the byte with the bitmask 0x20 (0b00100000). Since there are 0's everywhere but the 5th bit, this operation will leave all the other bits unchanged (1 OR 0 is 1, 0 OR 0 is 0) while setting bit 5 (1 OR 1 is 1 and 0 OR 1 is 1).

It is generally considered bad practice to use "magic numbers" as bitmasks. You usually see people construct bitmasks using bitshift operators. For example, the above bitmask would be written as (1 << 5) (0b00000001 shifted left 5 times). Any modern compiler will optimize this back to a magic number in the machine code.

So here's how you would perform each of the four bit operations in C:

Code:
unsigned char myByte;

//set bit 5
myByte |= (1 << 5);

//clear bit 5 (Note how we need the negation of the previous bitmask here in order to clear bit 5 while leaving the rest unchanged.
//We need ones everywhere except for the bit we are trying to clear, because 1 AND 1 = 1 and 1 AND 0 = 0.)
myByte &= ~(1 << 5);

//Toggle bit 5
myByte ^= (1 << 5);

//Test bit 5
if(myByte & (1 << 5)) { printf("bit 5 set!"); }


That's pretty much all there is to it. C also has bitfields which lets you name specific sets of bits in a larger structure.



Cornflake
Administrator
Administrator

User avatar

Joined: 30 Oct 2010
Gender: Male
Posts: 66,230
Location: Over there

16 Jul 2011, 10:43 am

And related to what mcg says, have a look at this for the underlying bits (pun intended!): http://en.wikipedia.org/wiki/Bitwise_operation
If you can see what's happening with the various bit states when subjected to and/or/not etc. operations then you've pretty much got it, and it's then just a case of representing those operations in your language of choice.


_________________
Giraffe: a ruminant with a view.


Madbones
Veteran
Veteran

User avatar

Joined: 7 Mar 2010
Age: 27
Gender: Male
Posts: 777
Location: In the zone

16 Jul 2011, 11:26 am

Cornflake wrote:
And related to what mcg says, have a look at this for the underlying bits (pun intended!): http://en.wikipedia.org/wiki/Bitwise_operation
If you can see what's happening with the various bit states when subjected to and/or/not etc. operations then you've pretty much got it, and it's then just a case of representing those operations in your language of choice.

Thanks MCG, Cornflake.
I would have searched it, but did not know what to search for.