skellt

skellt

Apple Game Frameworks and Technologies: Wrote binary, meant decimal (p 85)

At the bottom of page 85 is this info box:

By default, integer literals are expressed in decimal using the numbers 0 through 9. However, you can modify this default behavior and use binary literals instead, which might make it easier. With binary literals, you use the prefix 0b. Unlike integer literals, binary literals use only 0 and 1. So, in this example, 0b100 equals the number four. Essentially, this is shorthand for 000000100. If you choose to use binary literals, you’ll need to work in squares: 1, 2, 4, 8, 16, and so on.

The last sentence says If you choose to use binary literals, you’ll need to work in squares: 1, 2, 4, 8, 16, and so on. I think that’s a typo and was meant to read: “If you choose to use decimal literals…”

Marked As Solved

Paradox927

Paradox927

Author and Editor at PragProg

Hello, David.

An integer literal represents some arbitrary value. You can express this value as a decimal, binary, octal, or hexadecimal constant. Binary literals begin with 0b, octal literals begin with 0o, and hexadecimal literals begin with 0x. Decimal literals have no prefix—they are what they are.

In this case, the physics category is a single 32-bit integer that acts as a mask, and each bit represents a single category, which is why you’re limited to 32 categories per scene. With masking, using powers of two makes things easier—especially when using binary literals.

Now, having said that, what you pointed out wasn’t actually a typo, but the information was certainly misleading. What it should have read–and now does read–is as follows:

When setting your categories, it’s best to do so using powers of two: 1, 2, 4, 8, 16, and so on.

You’ll see this new explanation in the next update.

Also, later in the book, I explain how to create and use a Hashable OptionSet to set up and retrieve the physics category—something that is also mentioned in that same aside. Enums are great, and for simpler games, preferred. However, as your game grows in complexity, Hashable OptionSets provide a bit more flexibility. You’ll see this solution in use when you get to Val’s Revenge.

Thanks again for pointing this out. Have a great day.

Also Liked

skellt

skellt

When setting your categories, it’s best to do so using powers of two: 1, 2, 4, 8, 16, and so on.

Yes I agree this will read better. Regardless of which system we use, the mask values ought to be 1, 2, 4, 8, 16 etc… In decimal we will actually use the literals 1, 2, 4, 8, 16.

In binary we will be representing those values using only 0, 1, and the letter b (e.g. 0b100 for 4). It was confusing previously when it said that if you choose to use binary you’ll need to work with 1, 2, 4. You were referring to the mask values, I thought you were referring to the literals. The new text makes it clear you are referring to the category values.

Popular Prag Prog topics Top

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
johnp
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’. In particula...
New
mikecargal
Title: Hands-On Rust (Chap 8 (Adding a Heads Up Display) It looks like ​.with_simple_console_no_bg​(SCREEN_WIDTH*2, SCREEN_HEIGHT*2...
New
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex> product = %Product{} %Pento....
New
alanq
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New
Chrichton
Dear Sophie. I tried to do the “Authorization” exercise and have two questions: When trying to plug in an email-service, I found the ...
New
swlaschin
The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New
curtosis
Running mix deps.get in the sensor_hub directory fails with the following error: ** (Mix) No SSH public keys found in ~/.ssh. An ssh aut...
New
hazardco
On page 78 the following code appears: <%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
a.zampa
@mfazio23 I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
New

Other popular topics Top

DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
PragmaticBookshelf
Author Spotlight: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
New
PragmaticBookshelf
Author Spotlight: Bruce Tate @redrapids Programming languages always emerge out of need, and if that’s not always true, they’re defin...
New
New

Latest in PragProg

View all threads ❯