4 ms·
How does bit shifting prevent typos as opposed to just explicit numbering?
by darkxanthos 10y ago
How does bit shifting prevent typos as opposed to just explicit numbering?
- Benjamin_Dobell 10y agoIt may or may not. However, what's more important is the semantic significance. You're making it clear these are flags and you're concerned with individual bits. 2, 4, 8 etc. are decimal numbers and mean something else i.e. "two", "four", "eight". Does bitwise ORing the decimal numbers 2 and 4 really make sense? Not particularly, but combining bits at certain offset does.
- NKCSS 10y agoBit off-topic, but in C# at least you can mark your enums with the 'Flags' attribute to indicate it's a flags enum.
- revelation 10y agoThe odd thing here is that these don't seem to be flags.. can a glass be both shattered and shattering?
- Benjamin_Dobell 10y agoTime for a pull request? ;)
- NKCSS 10y agoMaybe you read it wrong :) See http://csharppad.com/ http://csharppad.com/ // State flags enum EGlassRNState { EGlassRNState_Initial = 0, EGlassRNState_Weakened = 1 << 0, EGlassRNState_Shattering = 1 << 1, EGlassRNState_Shattered = 1 << 2, EGlassRNState_ActiveFrags = 1 << 3 }; foreach(var val in Enum.GetValues(typeof(EGlassRNState ))) { Console.WriteLine($"{val} -> {(int)val}"); } Output: EGlassRNState_Initial -> 0 EGlassRNState_Weakened -> 1 EGlassRNState_Shattering -> 2 EGlassRNState_Shattered -> 4 EGlassRNState_ActiveFrags -> 8
- Kurtz79 10y agoI think he meant that in this context does not make sense for more than one value to be true at the same time , which is the reason you define enums this way, usually.
- ghurtado 10y agoNot necessarily. Enums main application is to restrict a variable to a range of valid values. As to whether some other object may have one or more of these vales, enum makes no statement.
- deleted 10y ago[deleted]
- fla 10y agoBut then using bit fields makes no sense.
- olalonde 10y agoIt can be used like this, no? writable = 001 readable = 010 readwrite = 011 (writable | readable)
- fla 10y agoYes yes, but the point was, it can't be Shattering and Shattered at the same time ;)
- viewer5 10y agoI throw a baseball through a window, and set the window's state to both Shattered and Shattering. It's Shattered, for the purposes of "is this an intact pane of glass?" e.g. if I throw another baseball, it's not going to shatter again, and I can jump through it without any physical resistance (in the case of strong/thick glass, if it was still intact I might risk bouncing off if I'm at low velocity or mass). But it's also still in the process of Shattering, so when I jump through it, I'm risking cuts, it's actively producing noise for someone to hear, that laser I'm shining at it will be fragmenting wildly rather than reflecting off/passing through/deflecting like it was before the glass started Shattering.
- deleted 10y ago[deleted]
- hajile 10y agoThere are three distinct values: unshattered, shattering, and shattered which appear to correspond to 00, 01, and 10 with 11 being an unused state. This will always occur in boolean with 2 bits and 3 used values (unless someone stupidly gave it an undocumented purpose).
- jtuente 10y agoIt could serve a dual function to describe what states are valid for a given piece of glass. e.g. EGlassRNState allowed = EGlassRNState_Initial | EGlassRNState_Shattered; EGlassRNState current = EGlassRNState_Initial;
- NKCSS 10y agoSpot the error: ... [DayOfMonth(28)] D28 = 134217728L, [DayOfMonth(29)] D29 = 268435456L, [DayOfMonth(30)] D30 = 536870902L, [DayOfMonth(31)] D31 = 1073741824L, [DayOfMonth(DayOfMonthAttribute.LastDayOfMonth)] Last = 2147483648L Vs ... [DayOfMonth(28)] D28 = 1 << 28, [DayOfMonth(29)] D29 = 1 << 29, [DayOfMonth(30)] D30 = 1 << 31, [DayOfMonth(31)] D31 = 1 << 31, [DayOfMonth(DayOfMonthAttribute.LastDayOfMonth)] Last = 1 << 32 Hint: It's with D30 in both cases...
- olalonde 10y agoWait, so you used to compute the bit shifted value in your calculator and type it out literally (in base 10) in your code? I could understand typing out the binary literal but this is weird. Or am I misunderstanding?
- NKCSS 10y agoIt's how all MS Press books that I have read have explained it in the past; same goes for .NET focused websites back in 2001-2004 (when I got started in .NET
- to3m 10y agoIt's the same number in decimal and binary :) I might be a bit weird, but I know the powers of 2 up to 2^20 in decimal. They've just snuck in there over the years, along with 2^24 and 2^31. So if I need some powers of two I might well just type them out like that. I can recognise a few common combinations too... it is all much easier in hex though. I should really get the full set up to 2^64 memorised...
- olalonde 10y agoThat's an impressive feat but now that you know about the bit shifting trick, I suggest you start using it because most of us mortals don't instantly recognise large powers of 2 when they're written out in decimal :) I'm still kind of amazed that you learned how to do bit arithmetic without coming across that pattern.