What's the use of "& 0xff" in programming? - Oscar Liang (2024)

by Oscar

When I strated out, I was really confused about bit shift andhexadecimal number and binary. This is a good example how confusing it could get sometimes.Occasionallyyou see this in the code, for exmaple in C++:

What's the use of "& 0xff" in programming? - Oscar Liang (1)
byte a = ( b >> 8) & 0xff; // b is an integer

Okay, I know what “>> 8” means, we are shifting variable “b” to the rightby 8 bits. But, what the heck is “& 0xff” doing there?

Well, 0xff is the hexadecimal number FF which has a integer value of 255. And the binary representation of FF is 00000000000000000000000011111111 (under the 32-bit integer).

The & operator performs a bitwise AND operation. a & b will give you an integer with a bit pattern that has a 0 in all positions where b has a 0, while in all positions where b has a 1, the corresponding bit value froma is used (this also goes the other way around). For example, the bitwise AND of 10110111 and00001101 is 00000101.

In a nutshell, “& 0xff”effectively masks the variable so it leaves only the value in the last 8 bits, and ignores all the rest of the bits.

It’s seen most in cases like when trying to transform color values from a special format to standard RGB values (which is 8 bits long).

Leave a Comment

6 comments

Shayan 14th April 2018 - 11:51 pm

I was confused about 0xff. this article helped me alot. thanks for sharing.

Reply

Hoswar Ruby 5th January 2017 - 5:01 am

i think , it’s really wierd …

Reply

Muslim Aswaja 15th March 2016 - 3:58 pm

Thank you, Oscar! It’s really helpfull for me.
:-)

Reply

Simon 18th November 2015 - 9:39 pm

Hi Oscar,

Thanks for your knowledge, it is really helpful to me!

Reply

Abder-Rahman 23rd August 2015 - 5:59 pm

Thanks for the nice tutorial!

Reply

bit_shift 2nd December 2014 - 9:47 pm

Forgive me, but isn’t it >> a right shift, because you know it point right and move the bits to the right, and << is the left shift?

Reply

What's the use of "& 0xff" in programming? - Oscar Liang (2024)
Top Articles
14 Jobs that Pay $200K a Year (or Close to It)
Types of Banking Frauds - Tips to Prevent
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Jackerman Mothers Warmth Part 3
Manhattan Prep Lsat Forum
Pitt Authorized User
Aiken County government, school officials promote penny tax in North Augusta
craigslist: south coast jobs, apartments, for sale, services, community, and events
Yi Asian Chinese Union
Nesb Routing Number
Here's how eating according to your blood type could help you keep healthy
Xrarse
Minn Kota Paws
Oppenheimer & Co. Inc. Buys Shares of 798,472 AST SpaceMobile, Inc. (NASDAQ:ASTS)
Hello Alice Business Credit Card Limit Hard Pull
litter - tłumaczenie słowa – słownik angielsko-polski Ling.pl
Oriellys St James Mn
2021 Lexus IS for sale - Richardson, TX - craigslist
Costco Gas Foster City
The Shoppes At Zion Directory
ᐅ Bosch Aero Twin A 863 S Scheibenwischer
Les Rainwater Auto Sales
Missouri Highway Patrol Crash
Talbots.dayforce.com
Little Caesars 92Nd And Pecos
Jeffers Funeral Home Obituaries Greeneville Tennessee
If you have a Keurig, then try these hot cocoa options
Vernon Dursley To Harry Potter Nyt Crossword
Boise Craigslist Cars And Trucks - By Owner
Turbo Tenant Renter Login
Pensacola Tattoo Studio 2 Reviews
Jurassic World Exhibition Discount Code
Lacey Costco Gas Price
Rays Salary Cap
Beth Moore 2023
Weekly Math Review Q4 3
Craigslist West Seneca
Games R Us Dallas
20 Best Things to Do in Thousand Oaks, CA - Travel Lens
Aliciabibs
Felix Mallard Lpsg
Noaa Duluth Mn
22 Golden Rules for Fitness Beginners – Barnes Corner Fitness
VerTRIO Comfort MHR 1800 - 3 Standen Elektrische Kachel - Hoog Capaciteit Carbon... | bol
Lorton Transfer Station
Ratchet And Clank Tools Of Destruction Rpcs3 Freeze
Wood River, IL Homes for Sale & Real Estate
Is Chanel West Coast Pregnant Due Date
Besoldungstabellen | Niedersächsisches Landesamt für Bezüge und Versorgung (NLBV)
Free Carnival-themed Google Slides & PowerPoint templates
Latest Posts
Article information

Author: Wyatt Volkman LLD

Last Updated:

Views: 6302

Rating: 4.6 / 5 (66 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Wyatt Volkman LLD

Birthday: 1992-02-16

Address: Suite 851 78549 Lubowitz Well, Wardside, TX 98080-8615

Phone: +67618977178100

Job: Manufacturing Director

Hobby: Running, Mountaineering, Inline skating, Writing, Baton twirling, Computer programming, Stone skipping

Introduction: My name is Wyatt Volkman LLD, I am a handsome, rich, comfortable, lively, zealous, graceful, gifted person who loves writing and wants to share my knowledge and understanding with you.