setting a specific bit

Programming, for all ages and all languages.
Post Reply
slacker

setting a specific bit

Post by slacker »

how would i go about setting an individual bit...on the keyboard controller i want to set a certain bit to turn on a LED light without messing with the other bits.
Mr_Spam

Re:setting a specific bit

Post by Mr_Spam »

ive also been thinking of some LEDS handeling algorythems... is there a way to read the status of which LEDS are on or off? then you could toggle that bit to change without chaning the state of the other leads.
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:setting a specific bit

Post by distantvoices »

You keep an global unsigned char-variable in your keyboard driver module: uchar_t led_state

it has 8 bits. we need the lower three:

000: everything off.

001: scroll on = 1
010: caps lock on = 2
100: num lock on = 4

say you want to set num lock:

led_state+=4.

to set caps lock: led_state+=2.

Now you have the following bit-mask:110

say you want to switch off num lock:

led_state-=4.

Now the bitmask looks like this: 010.

got it?
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:setting a specific bit

Post by Pype.Clicker »

i wouldn't use '+' and '-' to manipulate a bitfield, if i were you: trying to set bit 1 with x+=2 will modify bit 2 if bit 1 was already set ...

It's cleaner to use bitwise operators like '&' and '|'

x|=2 : sets bit 1, other bits unchanged
x&=(~2) : clears bit 1, other bits unchanged.
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:setting a specific bit

Post by distantvoices »

*gg*

It's a good method with bitwise operators, you are right.

this ~-operator, it INVERSES the bits, isn't it? Thus, ~2 would look like 101 in binary, and a field, say 110&101 will result in 100 - > bit 010 is erased. Did I understand this right?
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:setting a specific bit

Post by Pype.Clicker »

yes, its roughly this, except that ~2 will not be 101,but rather 11111101 (0xFD) if the expression is a char-sized, 0xFFFD if the expression is short-sized and 0xfffffffd if it's an int.

To know how such operators work, you must learn their truth tables. For instance,

1&1 == 1, 1&0==0, 0&1==0, 0&0==0.
--> a 0 in x will clear the corresponding bit in a&x,
a 1 in x keeps the bit from a.

1|1 == 1, 1|0==1, 0|1==1, 0|0==0.
--> a 1 in x will set the corresponding bit in a|x

1^1 == 0, 1^0==1, 0^1==1, 0^0==1.
--> a 1 in x will toggle the corresponding bit in a^x
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:setting a specific bit

Post by distantvoices »

truth tables:

AND (a&b) or (a&&b):
a and b result

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

OR(a|b) or (a||b):
a or b result
1 1 true
1 0 true
0 1 true
0 0 false

exclusive OR:
a XOR b result
1 1 false
1 0 true
0 1 true
0 0 false

Implication: also expressed as !a|b (NOT a OR b)
a implies b result
1 1 true
1 0 false
0 1 true
0 0 true

I didn't like the surreal experiments the teachers/professors did with these things, but I like using it in real nice nifty programs. These constructs can shorten else lenghty if structures.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
Mr_Spam

Re:setting a specific bit

Post by Mr_Spam »

ah the good old truth tables.... did thoes last year in math class. I finally have a use for them!
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

Re:setting a specific bit

Post by Perica »

..
Last edited by Perica on Sun Dec 03, 2006 8:59 pm, edited 1 time in total.
Peter_Vigren

Re:setting a specific bit

Post by Peter_Vigren »

Wait for the keyboard to accept commands. Send the command 0xED (put next byte into the output port) to port 0x60. After the keyboard once again is ready to accept a command, send the LED byte.

The 3 lesser bits (0:2) are the LED bits and the above must be zero.
slacker

Re:setting a specific bit

Post by slacker »

Pype.Clicker wrote: i wouldn't use '+' and '-' to manipulate a bitfield, if i were you: trying to set bit 1 with x+=2 will modify bit 2 if bit 1 was already set ...

It's cleaner to use bitwise operators like '&' and '|'

x|=2 : sets bit 1, other bits unchanged
x&=(~2) : clears bit 1, other bits unchanged.
what does the ~ do? i never learned about it....
and what does the &= and |& do?
Curufir

Re:setting a specific bit

Post by Curufir »

Well my C is a little hazy, but as far as I remember a tilde is the equivalent of a NOT gate.

So to run this through with only 8 bits you have:

Code: Select all

x&=(~2) : clears bit 1, other bits unchanged
You end up doing:

Code: Select all

x & 11111110
Which rather effectively clears the first bit.

Presumably the C compiler is smart enough to use the same number of bits in the 2 as in X, so all other bits will be left unchanged by the AND. This saves you the hassle of specifying a precise number to AND with at compile time (Magic numbers and relying on type size is bad practice).

Forgot this in the original post. +=, -= , |=, &= etc are all abbreviations for common mathematical/logical operations.

Eg x+=1 actually means x=x+1

The rest follow exactly the same form.

Standard C stuff, but as I mentioned I'm a little rusty on it, so if I got something wrong please correct the mistake.
Whatever5k

Re:setting a specific bit

Post by Whatever5k »

Curufir wrote: You end up doing:

Code: Select all

x & 11111110
Actually that's

Code: Select all

x = x & 11111101
Remember: 2 is 10 in binary and ~ just inverses the bits (1 gets to 0 and other way round).

x &= y <equal> x = x & y
x | = y <equal> x = x | y

same with other operators (+,-,/, etc.)
Curufir

Re:setting a specific bit

Post by Curufir »

Good catch Abless, serves me right for posting in the middle of the night :).
Post Reply