'How to clear bits using Bitwise Operators

I'm interested in learning how to clear bits so that I can use part of the binary value, instead of the entire thing. What I hope to use this for is to obtain the signed bit, exponent, and the significand from an IEEE single precision float form.

However, I'm not entirely familiar with the hexadecimal system, and I was wondering if I could get your help.

Here are my thoughts so far: Signed bit - Bitwise-AND the binary value by 0x100000000 to get the first bit only

Exponent - Shift the value left once, and bitwise-and the new value by 0xFF000000 to obtain the first eight bits for the exponent

Significand - Shift the value left 23 times

Since each of these will require doing work on the original value, I'm planning to also store the value into another register. That way, I can still do work on the value without "harming" the original. The value of the signed bit, exponent, and the signficand will then be stored into separate registers.



Solution 1:[1]

Here's some NASM syntax 32bit

MOV eax, DWORD [ssValue]
MOV ebx, DWORD [signMask] ;; 80000000h
MOV ecx, DWORD [expoMask] ;; 7F800000h
MOV edx, DWORD [sigfMask] ;; 007FFFFFh
AND ebx, eax
AND ecx, eax
AND edx, eax ;; edx is the significand
SHR ebx, 31 ;; ebx is the sign
SHR ecx, 23 ;; ecx is the exponent

In C it would be something like

int sign = value & 0x80000000;
int expo = value & 0x7F800000;
int sigf = value & 0x007FFFFF;
sign >>= 31;
expo >>= 23;

EDIT if you want to correct the exponent which has a 127 (7Fh) bias

if(expo == 0x0) {
    // denormal
} else if(expo == 0xFF) {
    // +inf or nan depending on sigf
} else { // -126 to 127 range
    expo -= 0x7F;
}

EDIT 2 In other languages like Java ">>" is a signed shift operator.

sign >>= 31; // would result in 0 or -1 (0xFFFFFFFF) if the sign bit was 1
sign >>>= 31; // would result in 0 or 1 

Solution 2:[2]

In java, Bitwise XOR operation can be used to clear bit. You can convert a specific from String to Integer using

I have written this function to clear the leftmost bit, irrespective of the sign.

public static void main(String[] args) {

    System.out.println(String.format( "-2 binary=%32s",Integer.toBinaryString(-2)));
    int dropped = toggleLeftMostBit(-2);
    System.out.println(String.format( "-2 dropped left bit val= %d\n   binary=%32s",dropped,Integer.toBinaryString(dropped)));

}


   private static int toggleLeftMostBit(int i)
{

    return  i ^  Integer.highestOneBit(i);

}
private static int toggleNthBit(int i, int n)
{
    return  i ^  1<< n-1;

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2