+ Post New Thread
Results 1 to 12 of 12
  1. #1
    Member level 1
    Points: 559, Level: 5

    Join Date
    Feb 2018
    Posts
    40
    Helped
    0 / 0
    Points
    559
    Level
    5

    setting bits in byte

    given page https://www.geeksforgeeks.org/set-cl...a-number-in-c/

    N = N | 1 << K

    where K is the bit that is to be set

    N = 1011 0101

    set the 6th bit of N

    N = 1011 0101 | 1 << 6


    What happens first OR th operation will perform or Bit will shift first

  2. #2
    Full Member level 3
    Points: 1,926, Level: 10

    Join Date
    Jul 2014
    Location
    West Coast
    Posts
    180
    Helped
    32 / 32
    Points
    1,926
    Level
    10

    Re: setting bits in byte

    The compiler may just look at the expression and then use a machine code "set bit" instruction, so, no shift will actually take place. Look at the assembly language listing the compiler creates to confirm how it is actually executing the expression. This may vary from compiler to compiler, and of course depend on the assembly language instruction set of the CPU.



    •   AltAdvertisement

        
       

  3. #3
    Super Moderator
    Points: 30,261, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    9,055
    Helped
    1142 / 1142
    Points
    30,261
    Level
    42
    Blog Entries
    9

    Re: setting bits in byte

    Quote Originally Posted by ansh11 View Post
    first OR th operation will perform or Bit will shift first
    Review C language fundamentals and search for "operator precedence chart" and you will realize that shift operations have precedence over logical operations; to avoid misinterpretation, whenever possible enclose with "(...)" expressions that need to be evaluated first.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



    •   AltAdvertisement

        
       

  4. #4
    Super Moderator
    Points: 265,324, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    46,357
    Helped
    14108 / 14108
    Points
    265,324
    Level
    100

    Re: setting bits in byte

    This may vary from compiler to compiler, and of course depend on the assembly language instruction set of the CPU.
    There are a few points that are marked as implementation dependent in C language reference, but not this basic operator precedence thing. If the compiler does not follow the mandatory behavior, you should return it and call in your money.



    •   AltAdvertisement

        
       

  5. #5
    Super Moderator
    Points: 30,261, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    9,055
    Helped
    1142 / 1142
    Points
    30,261
    Level
    42
    Blog Entries
    9

    Re: setting bits in byte

    Quote Originally Posted by ansh11 View Post
    N = N | 1 << K
    Worthy to mention that you did not copied correctly the expression from the link:

    return (n | (1 << (k - 1)))
    In your above case, the result would not be the SET BIT opperation as expected.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  6. #6
    Member level 1
    Points: 559, Level: 5

    Join Date
    Feb 2018
    Posts
    40
    Helped
    0 / 0
    Points
    559
    Level
    5

    Re: setting bits in byte

    Quote Originally Posted by andre_teprom View Post
    Review C language fundamentals and search for "operator precedence chart" and you will realize that shift operations have precedence over logical operations; to avoid misinterpretation, whenever possible enclose with "(...)" expressions that need to be evaluated first.
    Two operators are required to set a bet. logical OR, left shift operator

    Bitwise OR 1100 0010 | 0111 1100 = 1111 1110

    Bitwise left shift 1100 0010 << 1 = 100 00100


    I have to use both operators together now. How to set a bit?



  7. #7
    Super Moderator
    Points: 30,261, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    9,055
    Helped
    1142 / 1142
    Points
    30,261
    Level
    42
    Blog Entries
    9

    Re: setting bits in byte

    Confusing question, review post #5 anyway, there you can find both opperators together (in a single expression, if you meant that); anyway here it is as perhaps you want in C-format :

    Code:
    n = (n | (1 << (k - 1)))
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  8. #8
    Member level 1
    Points: 559, Level: 5

    Join Date
    Feb 2018
    Posts
    40
    Helped
    0 / 0
    Points
    559
    Level
    5

    Re: setting bits in byte

    Quote Originally Posted by andre_teprom View Post
    Confusing question, review post #5 anyway, there you can find both opperators together (in a single expression, if you meant that); anyway here it is as perhaps you want in C-format :

    Code:
    n = (n | (1 << (k - 1)))

    I understand that if I have to set a bit in the byte, then what would the result?

    assume N = 1100 0010

    7th bit = 1
    6th bit = 1
    5th bit = 0
    4th bit = 0
    3rd bit = 0
    2nd bit =0
    1st bit = 1
    last bit = 0

    set 5th bit so N = 1110 0010

    set 2nd bit so N = 1100 0110


    Code:
    N = (N | (1 << (K - 1)))
    N = 1100 0010 |(1 << (K -1)


    What will be the value of K if I want to set the 5th bit position?



  9. #9
    Super Moderator
    Points: 30,261, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    9,055
    Helped
    1142 / 1142
    Points
    30,261
    Level
    42
    Blog Entries
    9

    Re: setting bits in byte

    What will be the value of K if I want to set the 5th bit position?
    Code:
    K-1 = 5
    k = 5 - (-1)
    k=6
    I'm sure you could have inferred this by yourself.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  10. #10
    Member level 1
    Points: 559, Level: 5

    Join Date
    Feb 2018
    Posts
    40
    Helped
    0 / 0
    Points
    559
    Level
    5

    Re: setting bits in byte

    Quote Originally Posted by andre_teprom View Post
    Code:
    K-1 = 5
    k = 5 - (-1)
    k=6
    I'm sure you could have inferred this by yourself.
    N = 1100 0010 |(1 << 6)
    N = 1100 0010 | 0000 0001 << 6
    N = 1100 0010 | 01000000
    N = 1100 0010

    5th bit is not set, N should be 1110 0010



  11. #11
    Super Moderator
    Points: 81,905, Level: 69
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,627
    Helped
    3764 / 3764
    Points
    81,905
    Level
    69

    Re: setting bits in byte

    Hi

    In a byte there are 8 bits.
    Usually they are called bit 0 up to bit 7 in descending order (7654 3210)
    If you use this nomenclature (bit0 ... bit7) then
    * if you want to set bit 5 you have to use "5" as shift operator. 1 << 5

    To avoid confusion we prefer this nomenclature.

    ***********
    In your example
    You use "5th" bit as an equivalent to "bit5". But then you need to call the most right bit "0th bit". This is quite unusual to have a "0th something".
    No one would say "I am the 0th son of my parents".

    Klaus
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



    •   AltAdvertisement

        
       

  12. #12
    Member level 1
    Points: 559, Level: 5

    Join Date
    Feb 2018
    Posts
    40
    Helped
    0 / 0
    Points
    559
    Level
    5

    Re: setting bits in byte

    Quote Originally Posted by KlausST View Post
    Hi
    Usually they are called bit 0 up to bit 7 in descending order (7654 3210)

    Klaus
    I think I understood how does bit set in byte

    Byte = 1100 0010 (MSB to LSB) (76543210)

    0000 0001 < 1 = 00000010
    0000 0001 < 2 = 00000100
    0000 0001 < 3 = 00001000
    0000 0001 < 4 = 00010000
    0000 0001 < 5 = 001000000
    0000 0001 < 6 = 01000000
    0000 0001 < 7 = 10000000


    Set Bit 7th
    Code C - [expand]
    1
    2
    3
    
    N = 1100 0010 | 0000 0001 < 7
    N = 1100 0010 |  10000000
    N =1100 0010

    Set Bit 6th
    Code C - [expand]
    1
    2
    3
    
    1100 0010 | 0000 0001 < 6
    1100 0010 | 01000000
    1100 0010

    Set Bit 5th
    Code C - [expand]
    1
    2
    3
    4
    
    1100 0010 | 0000 0001 < 5
     
    N = 1100 0010 | 001000000
    N = 1110 0010



--[[ ]]--