Toggle search
Search
Toggle menu
notifications
Toggle personal menu
Editing
Spec.isa
(section)
From Turing Complete
Views
Read
Edit
Edit source
View history
associated-pages
Page
Discussion
More actions
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
Advanced
Special characters
Help
Heading
Level 2
Level 3
Level 4
Level 5
Format
Insert
Latin
Latin extended
IPA
Symbols
Greek
Greek extended
Cyrillic
Arabic
Arabic extended
Hebrew
Bangla
Tamil
Telugu
Sinhala
Devanagari
Gujarati
Thai
Lao
Khmer
Canadian Aboriginal
Runes
Á
á
À
à
Â
â
Ä
ä
Ã
ã
Ǎ
ǎ
Ā
ā
Ă
ă
Ą
ą
Å
å
Ć
ć
Ĉ
ĉ
Ç
ç
Č
č
Ċ
ċ
Đ
đ
Ď
ď
É
é
È
è
Ê
ê
Ë
ë
Ě
ě
Ē
ē
Ĕ
ĕ
Ė
ė
Ę
ę
Ĝ
ĝ
Ģ
ģ
Ğ
ğ
Ġ
ġ
Ĥ
ĥ
Ħ
ħ
Í
í
Ì
ì
Î
î
Ï
ï
Ĩ
ĩ
Ǐ
ǐ
Ī
ī
Ĭ
ĭ
İ
ı
Į
į
Ĵ
ĵ
Ķ
ķ
Ĺ
ĺ
Ļ
ļ
Ľ
ľ
Ł
ł
Ń
ń
Ñ
ñ
Ņ
ņ
Ň
ň
Ó
ó
Ò
ò
Ô
ô
Ö
ö
Õ
õ
Ǒ
ǒ
Ō
ō
Ŏ
ŏ
Ǫ
ǫ
Ő
ő
Ŕ
ŕ
Ŗ
ŗ
Ř
ř
Ś
ś
Ŝ
ŝ
Ş
ş
Š
š
Ș
ș
Ț
ț
Ť
ť
Ú
ú
Ù
ù
Û
û
Ü
ü
Ũ
ũ
Ů
ů
Ǔ
ǔ
Ū
ū
ǖ
ǘ
ǚ
ǜ
Ŭ
ŭ
Ų
ų
Ű
ű
Ŵ
ŵ
Ý
ý
Ŷ
ŷ
Ÿ
ÿ
Ȳ
ȳ
Ź
ź
Ž
ž
Ż
ż
Æ
æ
Ǣ
ǣ
Ø
ø
Œ
œ
ß
Ð
ð
Þ
þ
Ə
ə
Formatting
Links
Headings
Lists
Files
References
Discussion
Description
What you type
What you get
Italic
''Italic text''
Italic text
Bold
'''Bold text'''
Bold text
Bold & italic
'''''Bold & italic text'''''
Bold & italic text
==== Constructing a mask ==== Using hexits or even bits to describe a mask is easy enough if you're working with fairly small constant values (a full 64-bit mask is 16 hexits! That's a lot to write out already, never mind trying to write it in binary!) If you're working with non-constant values however, or if you just don't want to manually work out such large numbers, you can create your own mask with shifts and ORs. For example, if you want the 38th bit, you can simply use: <pre> %mask = 1 << 38 </pre> {{note|type=info|This shift operation counts bits starting at 0, so your LSB will be <code>1 << 0</code>, not <code>1 << 1</code>, despite it often being referred to as the "first" bit.}} If you want the three bits from the 36th through the 38, you can OR them together like so: <pre> %bit36 = 1 << 36 %bit37 = 1 << 37 %bit38 = 1 << 38 %mask = %bit36 | %bit37 | %bit38 </pre> That is obviously going to get very cumbersome if you want to extract 8 or 16 or more bits. We can use a nice property of binary numbers to simplify this however: <code>1 << n</code> has a single bit set at position <code>n</code>, so <code>(1 << n)-1</code> has that single bit cleared and all lowest <code>n</code> bits set: <pre> %mask = (1<<39)-1 </pre> will return a mask with the lowest 39 bits enabled. {{note|type=info|We want to include %bit38 from the prior example, so we need to go one higher before subtracting!}} But we can do better! If we want to recreate the 3-bit mask from above, we can construct it by using a second mask to "turn off" some of the lower bits from the first mask: <pre> %hi_mask = (1<<39)-1 %lo_mask = (1<<36)-1 %mask = %hi_mask ^ %lo_mask </pre> This initially creates <code>%hi_mask</code> with the lowest 39 bits enabled, as before. It then creates <code>%lo_mask</code> with only the lowest 36 bits enabled. The XOR operation performs the task of "turning off" the lower 36 bits: * bits 0..35 are turned off because <code>1^1 = 0</code> * bits 36..38 remain on because <code>1^0 = 1</code> * bits 39..63 remain off because <code>0^0 = 0</code> Finally, we can perform a shift to fully encapsulate the bit slicing behavior. In fact, we don't even need to "turn off" the lower bits this time, as we'll be shifting them out: <pre> %mask = (1<<39)-1 %value = 0x123456789ABCDEF %extracted = (%value & %mask) >> 36 </pre>
Summary:
Please note that all contributions to Turing Complete are considered to be released under the Creative Commons Attribution-ShareAlike (see
TuringComplete:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)