Ans Ans - 1 year ago 67
Java Question

using 1's and 0's of a binary code separately

I have an integer variable that contains a binary number.
but I need to know how to use each number of the binary number (i.e. the 1 or 0) individually to perform a task accordingly.

To make it clearer I am working on a flashing application and I need to Turn the flash on if the number is 1 with a specific delay and turn the flash off if the number is 0 for a specific delay.

for example; the number "11" will be converted into "1011", then the flash will be turned on twice the delay time and turn off once then turned of once.
How can the 1's and 0's of a binary number separately?

Answer Source
int value = 0b1101;
for (int i = 0; i < Integer.SIZE; i++) {
    if (((value << i) & 1) == 1) {
        // turn on
    } else {
        // turn off
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download