2016-01-18 4 views
5

विशिष्ट बिट जाओ मैं एक UInt32 चर की तरह 3238844000.से uint32

अब मैं पहले दो बिट्स के बाद इस संख्या के पहले दो बिट और 6 बिट प्राप्त करना चाहते है। दोनों बिट्स int होना चाहिए।

Decimal: 3238844000 
Binary: 11000001000011001101011001100000 
     ^^ 

और

Decimal: 3238844000 
Binary: 11000001000011001101011001100000 
      ^^^^^^ 

उत्तर

11

अद्यतन 2:

सरल (और यह भी सबसे तेजी से) इस मामले के लिए जिस तरह से पता चला है विशुद्ध रूप से का उपयोग करके होने के लिए बिटवाई-शिफ्ट ऑपरेटर

int val = (int)(input >> 30); // performs the same 
int val2 = (int)((input << 2) >> 26); //the simplest and the fastest way 

मैंने सुना है कि उस बिटवाई-शिफ्ट ऑपरेशंस तेजी से होने लगते हैं। लेकिन आज, जिज्ञासा से बाहर *, मैंने वास्तव में बिटवाई-शिफ्ट + मास्क ((int)((input & mask2) >> 24)) के बीच प्रदर्शन की तुलना में बिटवाई-शिफ्ट अकेले ((int)((input << 2) >> 26)) के साथ तुलना की। bitwise-shift अकेले ऑपरेशन लगभग 10% -15% से तेज़ है।

[2016-01-20 04:01:26.638 UTC] shift-mask: 235 ms shift-only: 199 ms 
[2016-01-20 04:01:30.402 UTC] shift-mask: 233 ms shift-only: 200 ms 
[2016-01-20 04:01:31.265 UTC] shift-mask: 233 ms shift-only: 198 ms 
[2016-01-20 04:01:32.116 UTC] shift-mask: 227 ms shift-only: 199 ms 
[2016-01-20 04:01:32.850 UTC] shift-mask: 233 ms shift-only: 198 ms 
[2016-01-20 04:01:33.584 UTC] shift-mask: 230 ms shift-only: 199 ms 
[2016-01-20 04:01:34.280 UTC] shift-mask: 263 ms shift-only: 214 ms 
[2016-01-20 04:01:35.055 UTC] shift-mask: 229 ms shift-only: 201 ms 
[2016-01-20 04:01:36.996 UTC] shift-mask: 234 ms shift-only: 201 ms 
[2016-01-20 04:01:37.933 UTC] shift-mask: 224 ms shift-only: 198 ms 
[2016-01-20 04:01:38.353 UTC] shift-mask: 222 ms shift-only: 196 ms 
[2016-01-20 04:01:38.798 UTC] shift-mask: 233 ms shift-only: 211 ms 
[2016-01-20 04:01:39.246 UTC] shift-mask: 235 ms shift-only: 213 ms 
[2016-01-20 04:01:39.668 UTC] shift-mask: 223 ms shift-only: 198 ms 
[2016-01-20 04:01:41.102 UTC] shift-mask: 234 ms shift-only: 200 ms 
[2016-01-20 04:01:41.524 UTC] shift-mask: 224 ms shift-only: 198 ms 
[2016-01-20 04:01:41.948 UTC] shift-mask: 223 ms shift-only: 200 ms 
[2016-01-20 04:01:42.373 UTC] shift-mask: 224 ms shift-only: 200 ms 
[2016-01-20 04:01:43.521 UTC] shift-mask: 233 ms shift-only: 197 ms 
[2016-01-20 04:01:44.272 UTC] shift-mask: 237 ms shift-only: 216 ms 
[2016-01-20 04:01:44.909 UTC] shift-mask: 231 ms shift-only: 196 ms 
[2016-01-20 04:01:45.353 UTC] shift-mask: 230 ms shift-only: 213 ms 
[2016-01-20 04:01:45.850 UTC] shift-mask: 237 ms shift-only: 207 ms 
[2016-01-20 04:01:46.276 UTC] shift-mask: 226 ms shift-only: 200 ms 
[2016-01-20 04:01:47.074 UTC] shift-mask: 234 ms shift-only: 203 ms 
[2016-01-20 04:01:47.718 UTC] shift-mask: 230 ms shift-only: 199 ms 
[2016-01-20 04:01:48.144 UTC] shift-mask: 226 ms shift-only: 200 ms 
[2016-01-20 04:01:48.567 UTC] shift-mask: 225 ms shift-only: 198 ms 
[2016-01-20 04:01:48.994 UTC] shift-mask: 225 ms shift-only: 199 ms 
[2016-01-20 04:01:49.429 UTC] shift-mask: 223 ms shift-only: 211 ms 
[2016-01-20 04:01:49.860 UTC] shift-mask: 232 ms shift-only: 198 ms 
[2016-01-20 04:01:50.284 UTC] shift-mask: 225 ms shift-only: 199 ms 

नोट::

इस परिणाम मुझे मिल गया है प्रत्येक प्रयोग (5,000,000 x 100) के संचालन के लिए किया जाता है।

* माइक्रो-नियंत्रकों से निपटने के अपने पुराने दिनों को याद रखना ...;)


मूल:

uint mask1 = 0xC0000000; //1100 0000 0000 0000 0000 0000 0000 0000 
uint mask2 = 0x3F000000; //0011 1111 0000 0000 0000 0000 0000 0000 
:

बस कैसे आप अपने UInt32 के लिए द्विआधारी प्रतिनिधित्व लगता है की तरह, आप अपने द्विआधारी प्रतिनिधित्व भी सही बिटवाइज़ मुखौटा खोजना चाहिए

और फिर उन्हें bitwise-और ओ के साथ उपयोग करें perator

पहले दो बिट्स पाने के लिए आपको बस इस तरह मुखौटा इस्तेमाल कर सकते हैं:

uint val = input & mask1; //should give you the first two bits, the rests are zero 

और अगले 6 बिट पाने के लिए: आप उन्हें int में की जरूरत है

uint val2 = input & mask2; //similarly, should give you only the six bits in the position which you want 

, फिर बस उन्हें कास्ट करें:

int val = (int)(input & mask1); 
int val2 = (int)(input & mask2); 

और यदि आप परिणाम एलएसबी (ली सेंट महत्वपूर्ण byte, दायीं 8-bit इस मामले में), बिटवाइज़ का उपयोग सही ऑपरेटर बदलाव:

int val = (int)((input & mask1) >> 30); //30 bits are 0 
int val2 = (int)((input & mask2) >> 24); //24 bits are 0 

अद्यतन:

के लिए जैसा कि ऊपर संस्करण में स्थानांतरित कर दिया है, वास्तव में, आप भी कर सकते थे बस bitwise दाएं शिफ्ट पहला वाला और लगभग दूसरे के लिए समान रूप से करें, सिवाय इसके कि इसे बिटवाई मास्क 0 की आवश्यकता होगी अवांछित पहले दो बिट्स को साफ़ करने के लिए(0011 1111)।

The first one: 

1100 0001 0000 1100 1101 0110 0110 0000 
--------------------------------------- >> 30 //bitwise right shift by 30 
0000 0000 0000 0000 0000 0000 0000 0011 //you get only the first two bits, the rests are all replaced by 0 

The second one: 

1100 0001 0000 1100 1101 0110 0110 0000 
--------------------------------------- >> 24 //bitwise right shift by 24 
0000 0000 0000 0000 0000 0000 1100 0001 
           0011 1111 //this is 0x3f 
--------------------------------------- & //this is bitwise-and 
0000 0000 0000 0000 0000 0000 0000 0001 //you only get the 6 bits which you want 

इस प्रकार आप अपने पहले मूल्य के लिए 3 (0000 0000 0000 0000 0000 0000 0000 0011) मिल जाएगा, और मिलती है:

int val = (int)(input >> 30); // performs the same 
int val2 = (int)((input >> 24) & 0x3F); //the simpler way 

क्या बिट प्रतिनिधित्व में उन्हें होने पालन (मैं तार्किक प्रवाह निम्नलिखित कम करने के लिए टिप्पणियां दे) है आपके दूसरे मूल्य के लिए 1 (0000 0000 0000 0000 0000 0000 0000 0001)

ऊपर दिए गए उदाहरण का पालन करने के साथ-साथ, मुझे लगता है कि आप इस बारे में विचार कर सकते हैं कि यह कई अन्य मामलों पर भी कैसे करें।

+0

स्थानांतरित संस्करण के लिए, स्थानांतरित करने के बाद मुखौटा के लिए बेहतर। मास्क को समझना आसान है। –

+0

@ मार्सेलो कैंटोस अपडेट, आपके इनपुट के लिए धन्यवाद! – Ian

+0

महान उत्तर – Philies

संबंधित मुद्दे