Unsetting the High Order bit of a char
Lo All,
Im trying to unset the high order bit of a character, you reckon this would do it?
i = i | 128;
I''ve tried but i dunno if the results im getting are right but m guessing they aint.
Thanks in advance....
Opppss... not xor.. and.. look at anonymous post......
Edited by - Gladiator on July 28, 2000 1:50:23 PM
Edited by - Gladiator on July 28, 2000 1:50:23 PM
July 28, 2000 12:41 PM
In C | is the bitwise OR operator and & is the bitwise AND operator, so to clear the high order bit you need to use the & operator like so:
i = i & 0x7F;
Or the abbreviated form
i &= 0x7F;
(This is assuming a character is 8 bits or course)
Quattro
i = i & 0x7F;
Or the abbreviated form
i &= 0x7F;
(This is assuming a character is 8 bits or course)
Quattro
July 28, 2000 12:59 PM
Thanks man does the 0x7F, just represent the number 128? Dont relly understand these hex or octal thingy me bobs
0x7f isn''t 128, it''s 127. i &= 0x7F doesn''t exactly clear the high bit, it but rather it retains the values of all the other bits. i &= ~0x80 would clear the high bit. Both bits of code have the same effect, but the second would make what you''re trying to do more clear to another reader of your code. You normally would only fiddle with bits declared as constants or defines, and not literal values. like i |= BIT1 | BIT2 | BIT4; and so on.
OK, assuming we''re working on an 8-bit char:
We''ve got 8-bits, right? So that''s a range of:
0000 0000 = 0 dec = 0 hex
1111 1111 = 255 dec = FF hex
OK, so what we want to do is clear the top bit. As they say in the song (Frampton? The Who? I can''t remember...), "One and One make One". Any other combo makes a 0. Since we want to zero out the top bit, we''ll set it to 0, and since we want to preserve everything else, we''ll set them to 1. So here''s our mask:
0111 1111 = 127 dec = 7F hex
Now then, we AND our mask and our character:
iiii iiii <- value in i
& 0111 1111 <- mask
-----------
0iii iiii <- i with high bit reset
And there it is...you can use similar techniques to SET bits (use OR) or FLIP bits (use XOR).
Vyvyan
We''ve got 8-bits, right? So that''s a range of:
0000 0000 = 0 dec = 0 hex
1111 1111 = 255 dec = FF hex
OK, so what we want to do is clear the top bit. As they say in the song (Frampton? The Who? I can''t remember...), "One and One make One". Any other combo makes a 0. Since we want to zero out the top bit, we''ll set it to 0, and since we want to preserve everything else, we''ll set them to 1. So here''s our mask:
0111 1111 = 127 dec = 7F hex
Now then, we AND our mask and our character:
iiii iiii <- value in i
& 0111 1111 <- mask
-----------
0iii iiii <- i with high bit reset
And there it is...you can use similar techniques to SET bits (use OR) or FLIP bits (use XOR).
Vyvyan
Because inverted bitfields can be confusing, we usually create our AND masks by inverting the bits we wish to clear:
i &= ~0x80; // clear high bit
Compiles to the same code, and it''s more readable. This is also a useful technique if you have several bits you want to clear:
#define HI_BIT 0x80
#define OTHER_BIT 0x04
i &= ~(HI_BIT | OTHER_BIT); // clears HI_BIT and OTHER_BIT
i &= ~0x80; // clear high bit
Compiles to the same code, and it''s more readable. This is also a useful technique if you have several bits you want to clear:
#define HI_BIT 0x80
#define OTHER_BIT 0x04
i &= ~(HI_BIT | OTHER_BIT); // clears HI_BIT and OTHER_BIT
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement