Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitwise right shift >> in Objective-C

I have a variable (unsigned int) part_1.

If I do this: NSLog(@"%u %08x", part_1, part_1); (print unsigned value, and hex value) it outputs:

2063597568 7b000000

(only first two will have values).

I want to convert this to

0000007b

So i've tried doing unsigned int part_1b = part_1 >> 6 (and lots of variations)

But this outputs:

32243712 01ec0000

Where am i going wrong?

like image 794
user2955517 Avatar asked Nov 05 '13 16:11

user2955517


People also ask

How does bitwise right shift work?

The bitwise shift operators move the bit values of a binary object. The left operand specifies the value to be shifted. The right operand specifies the number of positions that the bits in the value are to be shifted. The result is not an lvalue.

What is Bitshift?

Bitshifting shifts the binary representation of each pixel to the left or to the right by a pre-defined number of positions. Shifting a binary number by one bit is equivalent to multiplying (when shifting to the left) or dividing (when shifting to the right) the number by 2.

What does shift right do?

The right-shift operator causes the bit pattern in shift-expression to be shifted to the right by the number of positions specified by additive-expression . For unsigned numbers, the bit positions that have been vacated by the shift operation are zero-filled.


1 Answers

You want to shift by 6*4 = 24 bits, not just 6 bits. Each '0' in the hex printf represents 4 bits.

unsigned int part_1b = part_1 >> 24;
                                 ^^
like image 89
Charlie Burns Avatar answered Sep 28 '22 07:09

Charlie Burns