Stumped by bitwise calc

This is a discussion on Stumped by bitwise calc within the C Programming forums, part of the General Programming Boards category; Heres a test program: Code: #include <stdio.h> int main() { unsigned char reg = 128; char val = 6; reg ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    51

    Stumped by bitwise calc

    Heres a test program:
    Code:
    #include <stdio.h>
    
    int main()
    {
            unsigned char reg = 128;
            char val = 6;
    
            reg |= val & 128;
    
            printf("%02x\n", reg);
            return 0;
    }
    I am trying to clear the 7th bit of reg (has bits 0-7). I use 6 as an example... It doesnt matter since the 7th bit of 6 is 0 anyway. It doesn't clear, it still stays as 128 (should now be 0)

    Heres what does work though... (This is important)... If the 7th bit of val is 1, then obviously reg's 7th bit needs to be 1 as well. "| val & 128" handles this fine. Can anyone assist me with this? I have made a truth table as well:

    Code:
    reg    val     output
    0       1        1
    1       0        0
    0       0        0
    1       1        1

    Note, I am trying to do this all in one line if possible.

    Is it possible?

  2. #2
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Try

    #define ALL_oTHER_1S_EXCEPT_7TH_BIT (calculate number here)

    reg=(reg&ALL_oTHER_1S_EXCEPT_7TH_BIT);

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Isn't this what AND is for?
    Code:
    int main() {
    	unsigned char byte=128;
    	byte&=6;
    	printf("%d\n",byte);	
    }
    clears the last bit. I think you might as well just say byte&=0 tho
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    51
    MK27, Maz:

    & wont work because if the 7th bit of val is 1 and we & that with reg which 7ths bit is 0, the resulting value of reg would be 0. I need it to be 1.
    | wont work either because if the 7th bit of val is 0 and we | that with reg which 7th bit is 1, the resulting value of reg would be 1. I need it to be 0.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Looking at your truth table, the output is 1 if and only if val is 1. Basically, you are trying to set reg's 7th bit to val's 7th bit, right?

    I am not sure what is the best way of doing that, but one way is:
    Code:
    reg = (val & (1 << 7)) ? (reg | (1 << 7)) : (reg & ~(1 << 7));
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by someprogr View Post
    MK27, Maz:

    & wont work because if the 7th bit of val is 1 and we & that with reg which 7ths bit is 0, the resulting value of reg would be 0. I need it to be 1.
    You set the 7th bit to 1 already by making the number 128. If you want to change the 7th bit in some way, the only choice is to 0. If you do not want to change the 7th bit, why are you asking how change the 7th bit?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Your code
    Code:
            reg |= val & 128;
    works like this:

    Since the '&' oper takes precedence, val & 128 = 0x00. When you 'OR' it with 'reg' it becomes the 7th bit is one again!!!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by MK27
    If you do not want to change the 7th bit, why are you asking how change the 7th bit?
    As I noted in post #5, I think that someprogr is just trying to set the 7th bit of reg to the 7th but of val.

    If I am correct, this statement:
    "If the 7th bit of val is 1, then obviously reg's 7th bit needs to be 1 as well."
    should have been:
    "If the 7th bit of val is 1, then reg's 7th bit needs to be 1 as well; if the 7th bit of val is 0, then reg's 7th bit needs to be 0 as well."
    (Removing the "obviously" because it obviously was not obvious.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise Questions
    By someprogr in forum C Programming
    Replies: 8
    Last Post: 12-14-2008, 05:45 PM
  2. bitwise operations with double
    By henry_kay in forum C Programming
    Replies: 2
    Last Post: 10-03-2007, 04:57 AM
  3. C bitwise operator exercise - more like newb killer
    By shdwsclan in forum C Programming
    Replies: 3
    Last Post: 02-22-2006, 06:02 AM
  4. Characters into bitwise ints
    By Code Zer0 in forum C++ Programming
    Replies: 9
    Last Post: 04-24-2003, 08:34 AM
  5. Basic Calc
    By Universe in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2002, 10:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21