5

Jarret Hardie (thanks !) post this code yesterday to convert a NSinteget to binary, and works perfectly, but i need in 8bit format:

4 -> 00000100

any ideas to modify this code?

// Original author Adam Rosenfield... SO Question 655792
NSInteger theNumber = 56;
NSMutableString *str = [NSMutableString string];
for(NSInteger numberCopy = theNumber; numberCopy > 0; numberCopy >>= 1)
{
    // Prepend "0" or "1", depending on the bit
    [str insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
}

NSLog(@"Binary version: %@", str);

Thanks !!!!!!

3 Answers 3

12

This should work:

NSInteger theNumber = 56;
NSMutableString *str = [NSMutableString string];
NSInteger numberCopy = theNumber; // so you won't change your original value
for(NSInteger i = 0; i < 8 ; i++) {
    // Prepend "0" or "1", depending on the bit
    [str insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
    numberCopy >>= 1;
}

NSLog(@"Binary version: %@", str);
Sign up to request clarification or add additional context in comments.

1 Comment

great solution for logging bit mask values
5

If anybody's interested, here is my version that:

Snippet:

NSString *NSStringWithBits(int64_t mask) {
    NSMutableString *mutableStringWithBits = [NSMutableString new];
    for (int8_t bitIndex = 0; bitIndex < sizeof(mask) * 8; bitIndex++) {
        [mutableStringWithBits insertString:mask & 1 ? @"1" : @"0" atIndex:0];
        mask >>= 1;
    }
    return [mutableStringWithBits copy];
}

1 Comment

I guess it should be sizeof(mask) * 8 instead of sizeof(mask) since sizeof() returns bytes and you want bits ;)
3

In reference, and in support of @vincent osinga's answer.. Here is that code, wrapped in a C-function.. that returns the binary "string" from an NSUInteger.. perfect for logging bitwise typedef's, etc.

- (NSString*) bitString:(NSUInteger) mask{
    NSString *str = @"";
    for (NSUInteger i = 0; i < 8 ; i++) {
      // Prepend "0" or "1", depending on the bit
      str = [NSString stringWithFormat:@"%@%@", 
                      mask & 1 ? @"1" : @"0", str];
      mask >>= 1;
    }
    return str;
}

I don't think NSInteger numberCopy = theNumber; is necessary as you're not using a pointer, but simply the primitive value as an argument, // so you won't change your original value. This will enable use as / yield results like...

NSEventType anEvent = NSLeftMouseUp|NSLeftMouseDown;
NSLog(@"%@, %u\n%@, %u\n%@, %u\n%@, %u", 
    bitString( NSScrollWheel), NSScrollWheel,
    bitString( NSLeftMouseUp|NSLeftMouseDown),
               NSLeftMouseUp|NSLeftMouseDown, 
    bitString( anEvent ), anEvent, 
    bitString( NSAnyEventMask ), NSAnyEventMask);

NSLOG ➞

00010110, 22  /* NSScrollWheel */
00000011, 3   /* NSLeftMouseUp | NSLeftMouseDown */
00000011, 3   /* same results with typedef'ed variable */
11111111, 4294967295  /* NSAnyEventMask */

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.