Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

setting bits in byte

Status
Not open for further replies.
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.
 

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.
 

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.
 

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?
 

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)))
 

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?
 

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.
 

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
 

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
 

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

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top