in Education by
I use a byte to store some flag like 10101010, and I would like to know how to verify that a specific bit is at 1 or 0. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Here's a function that can be used to test any desired bit: bool is_bit_set(unsigned value, unsigned bitindex) { return (value & (1 << bitindex)) != 0; } A bit of explanation: The left shift operator (<<) is used to create a bit mask. (1 << 0) will be equal to 00000001, (1 << 1) will be equal to 00000010, (1 << 3) will be equal to 00001000, etc. So a shift of 0 tests the rightmost bit. A shift of 31 would be the leftmost bit of a 32-bit value. The bitwise-and operator (&) gives a result where all the bits that are 1 on both sides are set. Examples: 1111 & 0001 = 0001; 1111 & 0010 == 0010; 0000 & 0001 = 0000. So, the expression (value & (1 << bitindex)) will return the bitmask if the associated bit is 1 in value, or will return 0 if the associated bit is 0. Finally, we just check whether the result is non-zero. (This could actually be left out, but I like to make it explicit.)

Related questions

0 votes
    I use a byte to store some flag like 10101010, and I would like to know how to verify that a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I use a byte to store some flag like 10101010, and I would like to know how to verify that a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I use a byte to store some flag like 10101010, and I would like to know how to verify that a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    can you please please let me know how can I check whether on which mode (32 or 64 bit) my java ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    can you please please let me know how can I check whether on which mode (32 or 64 bit) my java ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 28, 2022 in Education by JackTerrance
0 votes
    can you please please let me know how can I check whether on which mode (32 or 64 bit) my java ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    This is my Xml document(small snippt). Table 11: Tanjavur NREGS activities CHAPTER IV: Findings and analysis ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    _______ flag bit is reset, when flag register content is 95H. Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
0 votes
    A byte addressable direct-mapped cache with 1024 blocks/lines, and with each block are having 8 32-bit words. How many bits are required ... 32-bit address? (a)10 (b)15 (c)3 (d)5...
asked Oct 6, 2020 in Technology by JackTerrance
0 votes
    How can I continue this rebase allowing unrelated histories with the forced flag introduced in the new release?...
asked Jan 8, 2021 in Technology by JackTerrance
0 votes
    c) A computer system was designed to have a good latest processor and I/O devices, however the designer of the ... in support of your Select the correct answer from above options...
asked Nov 28, 2021 in Education by JackTerrance
0 votes
0 votes
    Consider a series RLC circuit having resistance = 1Ω, capacitance = 1 F, considering that the capacitor gets charged ... EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    After reading the chapter, I know these points: I know that data gets highlighted after being selected. I know ... entry and editing. Select the correct answer from above options...
asked Nov 26, 2021 in Education by JackTerrance
...