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.

Where Next?

Popular Pragmatic Bookshelf topics Top

New
brianokken
Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New
sdmoralesma
Title: Web Development with Clojure, Third Edition - migrations/create not working: p159 When I execute the command: user=> (create-...
New
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex> product = %Product{} %Pento....
New
leba0495
Hello! Thanks for the great book. I was attempting the Trie (chap 17) exercises and for number 4 the solution provided for the autocorre...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
Charles
In general, the book isn’t yet updated for Phoenix version 1.6. On page 18 of the book, the authors indicate that an auto generated of ro...
New
akraut
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error. lib/pento_web/live/product_l...
New
rainforest
Hi, I’ve got a question about the implementation of PubSub when using a Phoenix.Socket.Transport behaviour rather than channels. Before ...
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
dimitarvp
Small essay with thoughts on macOS vs. Linux: I know @Exadra37 is just waiting around the corner to scream at me “I TOLD YOU SO!!!” but I...
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New

Sub Categories: