setting a specific bit
setting a specific bit
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.
Re:setting a specific bit
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.
-
- Member
- Posts: 1600
- Joined: Wed Oct 18, 2006 11:59 am
- Location: Vienna/Austria
- Contact:
Re:setting a specific bit
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?
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
BlueillusionOS iso image
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:setting a specific bit
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.
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.
-
- Member
- Posts: 1600
- Joined: Wed Oct 18, 2006 11:59 am
- Location: Vienna/Austria
- Contact:
Re:setting a specific bit
*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?
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
BlueillusionOS iso image
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:setting a specific bit
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
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
-
- Member
- Posts: 1600
- Joined: Wed Oct 18, 2006 11:59 am
- Location: Vienna/Austria
- Contact:
Re:setting a specific bit
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.
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
BlueillusionOS iso image
Re:setting a specific bit
ah the good old truth tables.... did thoes last year in math class. I finally have a use for them!
Re:setting a specific bit
..
Last edited by Perica on Sun Dec 03, 2006 8:59 pm, edited 1 time in total.
Re:setting a specific bit
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.
The 3 lesser bits (0:2) are the LED bits and the above must be zero.
Re:setting a specific bit
what does the ~ do? i never learned about it....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.
and what does the &= and |& do?
Re:setting a specific bit
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:
You end up doing:
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.
So to run this through with only 8 bits you have:
Code: Select all
x&=(~2) : clears bit 1, other bits unchanged
Code: Select all
x & 11111110
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.
Re:setting a specific bit
Actually that'sCurufir wrote: You end up doing:Code: Select all
x & 11111110
Code: Select all
x = x & 11111101
x &= y <equal> x = x & y
x | = y <equal> x = x | y
same with other operators (+,-,/, etc.)
Re:setting a specific bit
Good catch Abless, serves me right for posting in the middle of the night .